Skip to content

Module Belt.Array

Belt.Array

mutable array: Utilities functions

Belt.Array Utililites for Array functions

ocaml
type 'a t = 'a array
reasonml
type t('a) = array('a);
ocaml
val length : 'a t -> int
reasonml
let length: t('a) => int;

length xs return the size of the array

ocaml
val size : 'a t -> int
reasonml
let size: t('a) => int;

See length

ocaml
val get : 'a t -> int -> 'a option
reasonml
let get: t('a) => int => option('a);

get arr i

If i <= 0 <= length arr;returns Some value where value is the item at index i If i is out of range;returns None

ocaml
  Belt.Array.get [|"a";"b";"c"|] 0 = Some "a";;
  Belt.Array.get [|"a";"b";"c"|] 3 = None;;
  Belt.Array.get [|"a";"b";"c"|] (-1) = None;;
reasonml
Belt.Array.get([|"a", "b", "c"|], 0) == Some("a");
Belt.Array.get([|"a", "b", "c"|], 3) == None;
Belt.Array.get([|"a", "b", "c"|], -1) == None;
ocaml
val getExn : 'a t -> int -> 'a
reasonml
let getExn: t('a) => int => 'a;

getExn arr i

raise an exception if i is out of range;otherwise return the value at index i in arr

ocaml
val getUnsafe : 'a t -> int -> 'a
reasonml
let getUnsafe: t('a) => int => 'a;

getUnsafe arr i

Unsafe

no bounds checking;this would cause type error if i does not stay within range

ocaml
val getUndefined : 'a t -> int -> 'a Js.undefined
reasonml
let getUndefined: t('a) => int => Js.undefined('a);

getUndefined arr i

It does the samething in the runtime as getUnsafe; it is type safe since the return type still track whether it is in range or not

ocaml
val set : 'a t -> int -> 'a -> bool
reasonml
let set: t('a) => int => 'a => bool;

set arr n x modifies arr in place; it replaces the nth element of arr with x

returns false means not updated due to out of range

ocaml
val setExn : 'a t -> int -> 'a -> unit
reasonml
let setExn: t('a) => int => 'a => unit;

setExn arr i x raise an exception if i is out of range

ocaml
val setUnsafe : 'a t -> int -> 'a -> unit
reasonml
let setUnsafe: t('a) => int => 'a => unit;
ocaml
val shuffleInPlace : 'a t -> unit
reasonml
let shuffleInPlace: t('a) => unit;

shuffleInPlace arr randomly re-orders the items in arr

ocaml
val shuffle : 'a t -> 'a t
reasonml
let shuffle: t('a) => t('a);

shuffle xs

returns a fresh array with items in original array randomly shuffled

ocaml
val reverseInPlace : 'a t -> unit
reasonml
let reverseInPlace: t('a) => unit;

reverseInPlace arr reverses items in arr in place

ocaml
  let arr = [|10;11;12;13;14|];;
  let () = reverseInPlace arr;;
  arr = [|14;13;12;11;10|];;
reasonml
let arr = [|10, 11, 12, 13, 14|];
let () = reverseInPlace(arr);
arr == [|14, 13, 12, 11, 10|];
ocaml
val reverse : 'a t -> 'a t
reasonml
let reverse: t('a) => t('a);

reverse arr

returns a fresh array with items in arr in reverse order

ocaml
  reverse [|10;11;12;13;14|] = [|14;13;12;11;10|];;
reasonml
reverse([|10, 11, 12, 13, 14|]) == [|14, 13, 12, 11, 10|];
ocaml
val makeUninitialized : int -> 'a Js.undefined array
reasonml
let makeUninitialized: int => array(Js.undefined('a));

makeUninitialized n creates an array of length n filled with the undefined value. You must specify the type of data that will eventually fill the array.

ocaml
  let arr: string Js.undefined array = makeUninitialized 5;;
  getExn arr 0 = Js.undefined;;
reasonml
let arr: array(Js.undefined(string)) = makeUninitialized(5);
getExn(arr, 0) == Js.undefined;
ocaml
val makeUninitializedUnsafe : int -> 'a t
reasonml
let makeUninitializedUnsafe: int => t('a);

`makeUninitializedUnsafe n`

Unsafe

ocaml
let arr = Belt.Array.makeUninitializedUnsafe 5;;
let () = Js.log(Belt.Array.getExn arr 0);; (* undefined *)
Belt.Array.setExn arr 0 "example";;
let () = Js.log(Belt.Array.getExn arr 0 = "example");;
reasonml
let arr = Belt.Array.makeUninitializedUnsafe(5);
let () = Js.log(Belt.Array.getExn(arr, 0)); /* undefined */
Belt.Array.setExn(arr, 0, "example");
let () = Js.log(Belt.Array.getExn(arr, 0) == "example");
ocaml
val make : int -> 'a -> 'a t
reasonml
let make: int => 'a => t('a);

make n e return an array of size n filled with value e

returns an empty array when n is negative.

ocaml
val range : int -> int -> int t
reasonml
let range: int => int => t(int);

range start finish create an inclusive array

ocaml
  range 0 3 =  [|0;1;2;3|];;
  range 3 0 =  [||] ;;
  range 3 3 = [|3|];;
reasonml
range(0, 3) == [|0, 1, 2, 3|];
range(3, 0) == [||];
range(3, 3) == [|3|];
ocaml
val rangeBy : int -> int -> step:int -> int t
reasonml
let rangeBy: int => int => step:int => t(int);

rangeBy start finish ~step

returns empty array when step is 0 or negative it also return empty array when start > finish

ocaml
 rangeBy 0 10 ~step:3 = [|0;3;6;9|];;
 rangeBy 0 12 ~step:3 = [|0;3;6;9;12|];;
 rangeBy 33 0 ~step:1 =  [||];;
 rangeBy 33 0 ~step:(-1) = [||];;
 rangeBy 3 12 ~step:(-1) = [||];;
 rangeBy 3 3 ~step:0 = [||] ;;
 rangeBy 3 3 ~step:(1) = [|3|] ;;
reasonml
rangeBy(0, 10, ~step=3) == [|0, 3, 6, 9|];
rangeBy(0, 12, ~step=3) == [|0, 3, 6, 9, 12|];
rangeBy(33, 0, ~step=1) == [||];
rangeBy(33, 0, ~step=-1) == [||];
rangeBy(3, 12, ~step=-1) == [||];
rangeBy(3, 3, ~step=0) == [||];
rangeBy(3, 3, ~step=1) == [|3|];
ocaml
val makeByU : int -> (int -> 'a) Js.Fn.arity1 -> 'a t
reasonml
let makeByU: int => Js.Fn.arity1((int => 'a)) => t('a);
ocaml
val makeBy : int -> (int -> 'a) -> 'a t
reasonml
let makeBy: int => (int => 'a) => t('a);

makeBy n f

return an empty array when n is negative return an array of size n populated by f i start from 0 to n - 1

ocaml
  makeBy 5 (fun i -> i) = [|0;1;2;3;4|];;
  makeBy 5 (fun i -> i * i) = [|0;1;4;9;16|]
reasonml
makeBy(5, i => i) == [|0, 1, 2, 3, 4|];
makeBy(5, i => i * i) == [|0, 1, 4, 9, 16|];
ocaml
val makeByAndShuffleU : int -> (int -> 'a) Js.Fn.arity1 -> 'a t
reasonml
let makeByAndShuffleU: int => Js.Fn.arity1((int => 'a)) => t('a);
ocaml
val makeByAndShuffle : int -> (int -> 'a) -> 'a t
reasonml
let makeByAndShuffle: int => (int => 'a) => t('a);

makeByAndShuffle n f

Equivalent to shuffle (makeBy n f)

ocaml
val zip : 'a t -> 'b array -> ('a * 'b) array
reasonml
let zip: t('a) => array('b) => array(('a, 'b));

zip a b

Create an array of pairs from corresponding elements of a and b. Stop with the shorter array

ocaml
  zip [|1;2|] [|3;4;5|] = [|(1, 3);(2, 4)|]
reasonml
zip([|1, 2|], [|3, 4, 5|]) == [|(1, 3), (2, 4)|];
ocaml
val zipByU : 'a t -> 'b array -> ('a -> 'b -> 'c) Js.Fn.arity2 -> 'c array
reasonml
let zipByU: t('a) => array('b) => Js.Fn.arity2(('a => 'b => 'c)) => array('c);
ocaml
val zipBy : 'a t -> 'b array -> ('a -> 'b -> 'c) -> 'c array
reasonml
let zipBy: t('a) => array('b) => ('a => 'b => 'c) => array('c);

zipBy xs ys f

Create an array by applying f to corresponding elements of xs and ys Stops with shorter array

Equivalent to map (zip xs ys) (fun (a,b) -> f a b)

ocaml
  zipBy [|1;2;3|] [|4;5|] (fun a b -> 2 * a + b) = [|6;9|];;
reasonml
zipBy([|1, 2, 3|], [|4, 5|], (a, b) => 2 * a + b) == [|6, 9|];
ocaml
val unzip : ('a * 'b) array -> 'a t * 'b array
reasonml
let unzip: array(('a, 'b)) => (t('a), array('b));

unzip a takes an array of pairs and creates a pair of arrays. The first array contains all the first items of the pairs; the second array contains all the second items.

ocaml
  unzip [|(1,2) ; (3,4)|] = ([|1;3|], [|2;4|]);;
  unzip [|(1,2) ; (3,4) ; (5,6) ; (7,8)|] = ([|1;3;5;7|], [|2;4;6;8|]);;
reasonml
unzip([|(1, 2), (3, 4)|]) == ([|1, 3|], [|2, 4|]);
unzip([|(1, 2), (3, 4), (5, 6), (7, 8)|])
== ([|1, 3, 5, 7|], [|2, 4, 6, 8|]);
ocaml
val concat : 'a t -> 'a t -> 'a t
reasonml
let concat: t('a) => t('a) => t('a);

concat xs ys

returns a fresh array containing the concatenation of the arrays v1 and v2;so even if v1 or v2 is empty;it can not be shared

ocaml
  concat [|1;2;3|] [|4;5|] = [|1;2;3;4;5|];;
  concat [| |] [|"a";"b";"c"|] = [|"a";"b";"c"|];;
reasonml
concat([|1, 2, 3|], [|4, 5|]) == [|1, 2, 3, 4, 5|];
concat([||], [|"a", "b", "c"|]) == [|"a", "b", "c"|];
ocaml
val concatMany : 'a t t -> 'a t
reasonml
let concatMany: t(t('a)) => t('a);

concatMany xss

returns a fresh array as the concatenation of xss (an array of arrays)

ocaml
  concatMany [| [|1;2;3|]; [|4;5;6|]; [|7;8|] |] = [|1;2;3;4;5;6;7;8|];;
reasonml
concatMany([|[|1, 2, 3|], [|4, 5, 6|], [|7, 8|]|])
== [|1, 2, 3, 4, 5, 6, 7, 8|];
ocaml
val slice : 'a t -> offset:int -> len:int -> 'a t
reasonml
let slice: t('a) => offset:int => len:int => t('a);

slice xs offset len creates a new array with the len elements of xs starting at offset for

offset can be negative;and is evaluated as length xs - offset slice xs -1 1 means get the last element as a singleton array

slice xs (-len) len will return a copy of the array

if the array does not have enough data;slice extracts through the end of sequence.

if len is negative;returns the empty array.

ocaml
  slice [|10;11;12;13;14;15;16|] ~offset: 2 ~len: 3 = [|12;13;14|];;
  slice [|10;11;12;13;14;15;16|] ~offset: (-4) ~len: 3 = [|13;14;15|];;
  slice [|10;11;12;13;14;15;16|] ~offset:4  ~len:9 = [|14;15;16|];;
reasonml
slice([|10, 11, 12, 13, 14, 15, 16|], ~offset=2, ~len=3) == [|12, 13, 14|];
slice([|10, 11, 12, 13, 14, 15, 16|], ~offset=-4, ~len=3) == [|13, 14, 15|];
slice([|10, 11, 12, 13, 14, 15, 16|], ~offset=4, ~len=9) == [|14, 15, 16|];
ocaml
val sliceToEnd : 'a t -> int -> 'a t
reasonml
let sliceToEnd: t('a) => int => t('a);

sliceToEnd xs offset creates a new array with the elements of xs starting at offset

offset can be negative;and is evaluated as length xs - offset sliceToEnd xs -1 means get the last element as a singleton array

sliceToEnd xs 0 will return a copy of the array

ocaml
  sliceToEnd [|10;11;12;13;14;15;16|] 2 = [|12;13;14;15;16|];;
  sliceToEnd [|10;11;12;13;14;15;16|] (-4) = [|13;14;15;16|];;
reasonml
sliceToEnd([|10, 11, 12, 13, 14, 15, 16|], 2) == [|12, 13, 14, 15, 16|];
sliceToEnd([|10, 11, 12, 13, 14, 15, 16|], -4) == [|13, 14, 15, 16|];
ocaml
val copy : 'a t -> 'a t
reasonml
let copy: t('a) => t('a);

copy a

returns a copy of a;that is;a fresh array containing the same elements as a.

ocaml
val fill : 'a t -> offset:int -> len:int -> 'a -> unit
reasonml
let fill: t('a) => offset:int => len:int => 'a => unit;

fill arr ~offset ~len x

Modifies arr in place, storing x in elements number offset to offset + len - 1.

offset can be negative;and is evaluated as length arr - offset

fill arr ~offset:(-1) ~len:1 means fill the last element, if the array does not have enough data;fill will ignore it

ocaml

  let arr = makeBy 5 (fun i -> i) ;;
  fill arr ~offset:2 ~len:2 9 ;;
  arr = [|0;1;9;9;4|];;
  fill arr ~offset:7 ~len:2 8;;
  arr = [|0;1;9;9;4|];;
reasonml
let arr = makeBy(5, i => i);
fill(arr, ~offset=2, ~len=2, 9);
arr == [|0, 1, 9, 9, 4|];
fill(arr, ~offset=7, ~len=2, 8);
arr == [|0, 1, 9, 9, 4|];
ocaml
val blit : 
  src:'a t ->
  srcOffset:int ->
  dst:'a t ->
  dstOffset:int ->
  len:int ->
  unit
reasonml
let blit: 
  src:t('a) =>
  srcOffset:int =>
  dst:t('a) =>
  dstOffset:int =>
  len:int =>
  unit;

blit ~src:v1 ~srcOffset:o1 ~dst:v2 ~dstOffset:o2 ~len

copies len elements from array v1;starting at element number o1;to array v2, starting at element number o2.

It works correctly even if v1 and v2 are the same array;and the source and destination chunks overlap.

offset can be negative;-1 means len - 1;if len + offset is still negative;it will be set as 0

For each of the examples;presume that v1 = [|10;11;12;13;14;15;16;17|] and v2 = [|20;21;22;23;24;25;26;27|]. The result shown is the content of the destination array.

ocaml
  Belt.Array.blit ~src: v1 ~srcOffset: 4 ~dst: v2 ~dstOffset: 2 ~len: 3 |.
    [|20;21;14;15;16;25;26;27|]
  Belt.Array.blit ~src: v1 ~srcOffset: 4 ~dst: v1 ~dstOffset: 2 ~len: 3 |.
    [|10;11;14;15;16;15;16;17|]
reasonml
Belt.Array.blit(~src=v1, ~srcOffset=4, ~dst=v2, ~dstOffset=2, ~len=3)
->(
    [|20, 21, 14, 15, 16, 25, 26, 27|](
      Belt.Array.blit,
      ~src=v1,
      ~srcOffset=4,
      ~dst=v1,
      ~dstOffset=2,
      ~len=3,
    )
  )
->[|10, 11, 14, 15, 16, 15, 16, 17|];
ocaml
val blitUnsafe : 
  src:'a t ->
  srcOffset:int ->
  dst:'a t ->
  dstOffset:int ->
  len:int ->
  unit
reasonml
let blitUnsafe: 
  src:t('a) =>
  srcOffset:int =>
  dst:t('a) =>
  dstOffset:int =>
  len:int =>
  unit;

Unsafe blit without bounds checking

ocaml
val forEachU : 'a t -> ('a -> unit) Js.Fn.arity1 -> unit
reasonml
let forEachU: t('a) => Js.Fn.arity1(('a => unit)) => unit;
ocaml
val forEach : 'a t -> ('a -> unit) -> unit
reasonml
let forEach: t('a) => ('a => unit) => unit;

forEach xs f

Call f on each element of xs from the beginning to end. f returns unit;so no new array is created. Use forEach when you are primarily concerned with repetitively creating side effects.

ocaml
  forEach [|"a";"b";"c"|] (fun x -> Js.log("Item: " ^ x));;
  (*  prints:
    Item: a
    Item: b
    Item: c
  *)

  let total = ref 0;;
  forEach [|1;2;3;4|] (fun x -> total := !total + x);;
  !total  = 1 + 2 + 3 + 4;;
reasonml
forEach([|"a", "b", "c"|], x => Js.log("Item: " ++ x));
/*  prints:
      Item: a
      Item: b
      Item: c
    */

let total = ref(0);
forEach([|1, 2, 3, 4|], x => total := total^ + x);
total^ == 1 + 2 + 3 + 4;
ocaml
val mapU : 'a t -> ('a -> 'b) Js.Fn.arity1 -> 'b array
reasonml
let mapU: t('a) => Js.Fn.arity1(('a => 'b)) => array('b);
ocaml
val map : 'a t -> ('a -> 'b) -> 'b array
reasonml
let map: t('a) => ('a => 'b) => array('b);

map xs f

returns a new array by calling f for each element of xs from the beginning to end

ocaml
 map [|1;2|] (fun x-> x + 10) = [|11;12|]
reasonml
map([|1, 2|], x => x + 10) == [|11, 12|];
ocaml
val flatMapU : 'a t -> ('a -> 'b t) Js.Fn.arity1 -> 'b t
reasonml
let flatMapU: t('a) => Js.Fn.arity1(('a => t('b))) => t('b);
ocaml
val flatMap : 'a t -> ('a -> 'b t) -> 'b t
reasonml
let flatMap: t('a) => ('a => t('b)) => t('b);

flatMap xs f **return** a new array by calling `f` for each element of `xs` from the beginning to end, and then concatenating the results ``` flatMap |1;2| (fun x-> |x + 10;x + 20|) = |11;21;12;22| ```

ocaml
val getByU : 'a t -> ('a -> bool) Js.Fn.arity1 -> 'a option
reasonml
let getByU: t('a) => Js.Fn.arity1(('a => bool)) => option('a);
ocaml
val getBy : 'a t -> ('a -> bool) -> 'a option
reasonml
let getBy: t('a) => ('a => bool) => option('a);

getBy xs p returns Some value for the first value in xs that satisifies the predicate function p; returns None if no element satisifies the function.

ocaml
    getBy [|1;4;3;2|] (fun x -> x mod 2 = 0) = Some 4
    getBy [|15;13;11|] (fun x -> x mod 2 = 0) = None
ocaml
val getIndexByU : 'a t -> ('a -> bool) Js.Fn.arity1 -> int option
reasonml
let getIndexByU: t('a) => Js.Fn.arity1(('a => bool)) => option(int);
ocaml
val getIndexBy : 'a t -> ('a -> bool) -> int option
reasonml
let getIndexBy: t('a) => ('a => bool) => option(int);

getIndexBy xs p returns Some index for the first value in xs that satisifies the predicate function p; returns None if no element satisifies the function.

ocaml
    getIndexBy [|1;4;3;2|] (fun x -> x mod 2 = 0) = Some 1
    getIndexBy [|15;13;11|] (fun x -> x mod 2 = 0) = None
ocaml
val keepU : 'a t -> ('a -> bool) Js.Fn.arity1 -> 'a t
reasonml
let keepU: t('a) => Js.Fn.arity1(('a => bool)) => t('a);
ocaml
val keep : 'a t -> ('a -> bool) -> 'a t
reasonml
let keep: t('a) => ('a => bool) => t('a);

keep xs p

returns a new array that keeps all elements satisfying p

ocaml
  keep [|1;2;3|] (fun x -> x mod  2 = 0) = [|2|]
reasonml
keep([|1, 2, 3|], x => x mod 2 == 0) == [|2|];
ocaml
val keepWithIndexU : 'a t -> ('a -> int -> bool) Js.Fn.arity2 -> 'a t
reasonml
let keepWithIndexU: t('a) => Js.Fn.arity2(('a => int => bool)) => t('a);
ocaml
val keepWithIndex : 'a t -> ('a -> int -> bool) -> 'a t
reasonml
let keepWithIndex: t('a) => ('a => int => bool) => t('a);

keepWithIndex xs p

returns a new array that keeps all elements satisfying p. The predicate p takes two arguments: the element from xs and the index starting from 0.

ocaml
  keepWithIndex [|1;2;3|] (fun _x i -> i = 1) = [|2|]
reasonml
keepWithIndex([|1, 2, 3|], (_x, i) => i == 1) == [|2|];
ocaml
val keepMapU : 'a t -> ('a -> 'b option) Js.Fn.arity1 -> 'b array
reasonml
let keepMapU: t('a) => Js.Fn.arity1(('a => option('b))) => array('b);
ocaml
val keepMap : 'a t -> ('a -> 'b option) -> 'b array
reasonml
let keepMap: t('a) => ('a => option('b)) => array('b);

keepMap xs p

returns a new array that keeps all elements that return a non-None when applied to p

ocaml
  keepMap [|1;2;3|] (fun x -> if x mod 2 = 0 then Some x else None)
  = [| 2 |]
reasonml
keepMap([|1, 2, 3|], x =>
  if (x mod 2 == 0) {
    Some(x);
  } else {
    None;
  }
)
== [|2|];
ocaml
val forEachWithIndexU : 'a t -> (int -> 'a -> unit) Js.Fn.arity2 -> unit
reasonml
let forEachWithIndexU: t('a) => Js.Fn.arity2((int => 'a => unit)) => unit;
ocaml
val forEachWithIndex : 'a t -> (int -> 'a -> unit) -> unit
reasonml
let forEachWithIndex: t('a) => (int => 'a => unit) => unit;

forEachWithIndex xs f

The same as forEach; except that f is supplied with two arguments: the index starting from 0 and the element from xs

ocaml

  forEachWithIndex [|"a";"b";"c"|] (fun i x -> Js.log("Item " ^ (string_of_int i) ^ " is " ^ x));;
  (*  prints:
    Item 0 is a
    Item 1 is b
    Item 2 is c
  *)

  let total = ref 0 ;;
  forEachWithIndex [|10;11;12;13|] (fun i x -> total := !total + x + i);;
  !total  = 0 + 10 + 1 +  11 + 2 + 12 + 3 + 13;;
reasonml
forEachWithIndex([|"a", "b", "c"|], (i, x) =>
  Js.log("Item " ++ string_of_int(i) ++ " is " ++ x)
);
/*  prints:
      Item 0 is a
      Item 1 is b
      Item 2 is c
    */

let total = ref(0);
forEachWithIndex([|10, 11, 12, 13|], (i, x) => total := total^ + x + i);
total^ == 0 + 10 + 1 + 11 + 2 + 12 + 3 + 13;
ocaml
val mapWithIndexU : 'a t -> (int -> 'a -> 'b) Js.Fn.arity2 -> 'b t
reasonml
let mapWithIndexU: t('a) => Js.Fn.arity2((int => 'a => 'b)) => t('b);
ocaml
val mapWithIndex : 'a t -> (int -> 'a -> 'b) -> 'b t
reasonml
let mapWithIndex: t('a) => (int => 'a => 'b) => t('b);

mapWithIndex xs f applies f to each element of xs. Function f takes two arguments: the index starting from 0 and the element from xs.

ocaml
  mapWithIndex [|1;2;3|] (fun i x -> i + x) =
  [|0 + 1; 1 + 2; 2 + 3|]
reasonml
mapWithIndex([|1, 2, 3|], (i, x) => i + x) == [|0 + 1, 1 + 2, 2 + 3|];
ocaml
val partitionU : 'a t -> ('a -> bool) Js.Fn.arity1 -> 'a t * 'a t
reasonml
let partitionU: t('a) => Js.Fn.arity1(('a => bool)) => (t('a), t('a));
ocaml
val partition : 'a t -> ('a -> bool) -> 'a t * 'a t
reasonml
let partition: t('a) => ('a => bool) => (t('a), t('a));

partition f a split array into tuple of two arrays based on predicate f; first of tuple where predicate cause true, second where predicate cause false

ocaml
  partition [|1;2;3;4;5|] (fun x -> x mod 2 = 0  ) = ([|2;4|], [|1;2;3|]);;
  partition [|1;2;3;4;5|] (fun x -> x mod 2 <> 0 ) = ([|1;2;3|], [|2;4|]);;
reasonml
partition([|1, 2, 3, 4, 5|], x => x mod 2 == 0) == ([|2, 4|], [|1, 2, 3|]);
partition([|1, 2, 3, 4, 5|], x => x mod 2 != 0) == ([|1, 2, 3|], [|2, 4|]);
ocaml
val reduceU : 'b array -> 'a -> ('a -> 'b -> 'a) Js.Fn.arity2 -> 'a
reasonml
let reduceU: array('b) => 'a => Js.Fn.arity2(('a => 'b => 'a)) => 'a;
ocaml
val reduce : 'b array -> 'a -> ('a -> 'b -> 'a) -> 'a
reasonml
let reduce: array('b) => 'a => ('a => 'b => 'a) => 'a;

reduce xs init f

Applies f to each element of xs from beginning to end. Function f has two parameters: the item from the list and an “accumulator”;which starts with a value of init. reduce returns the final value of the accumulator.

ocaml
  reduce [|2;3;4|] 1 (+) = 10;;
  reduce [|"a";"b";"c";"d"|] "" (^) = "abcd";;
reasonml
reduce([|2, 3, 4|], 1, (+)) == 10;
reduce([|"a", "b", "c", "d"|], "", (++)) == "abcd";
ocaml
val reduceReverseU : 'b array -> 'a -> ('a -> 'b -> 'a) Js.Fn.arity2 -> 'a
reasonml
let reduceReverseU: array('b) => 'a => Js.Fn.arity2(('a => 'b => 'a)) => 'a;
ocaml
val reduceReverse : 'b array -> 'a -> ('a -> 'b -> 'a) -> 'a
reasonml
let reduceReverse: array('b) => 'a => ('a => 'b => 'a) => 'a;

reduceReverse xs init f

Works like reduce;except that function f is applied to each item of xs from the last back to the first.

ocaml
  reduceReverse [|"a";"b";"c";"d"|] "" (^) = "dcba";;
reasonml
reduceReverse([|"a", "b", "c", "d"|], "", (++)) == "dcba";
ocaml
val reduceReverse2U : 
  'a t ->
  'b array ->
  'c ->
  ('c -> 'a -> 'b -> 'c) Js.Fn.arity3 ->
  'c
reasonml
let reduceReverse2U: 
  t('a) =>
  array('b) =>
  'c =>
  Js.Fn.arity3(('c => 'a => 'b => 'c)) =>
  'c;
ocaml
val reduceReverse2 : 'a t -> 'b array -> 'c -> ('c -> 'a -> 'b -> 'c) -> 'c
reasonml
let reduceReverse2: t('a) => array('b) => 'c => ('c => 'a => 'b => 'c) => 'c;

reduceReverse2 xs ys init f Reduces two arrays xs and ys;taking items starting at min (length xs) (length ys) down to and including zero.

ocaml
  reduceReverse2 [|1;2;3|] [|1;2|] 0 (fun acc x y -> acc + x + y) = 6
reasonml
reduceReverse2([|1, 2, 3|], [|1, 2|], 0, (acc, x, y) => acc + x + y) == 6;
ocaml
val reduceWithIndexU : 'a t -> 'b -> ('b -> 'a -> int -> 'b) Js.Fn.arity3 -> 'b
reasonml
let reduceWithIndexU: 
  t('a) =>
  'b =>
  Js.Fn.arity3(('b => 'a => int => 'b)) =>
  'b;
ocaml
val reduceWithIndex : 'a t -> 'b -> ('b -> 'a -> int -> 'b) -> 'b
reasonml
let reduceWithIndex: t('a) => 'b => ('b => 'a => int => 'b) => 'b;

reduceWithIndex xs f

Applies f to each element of xs from beginning to end. Function f has three parameters: the item from the array and an “accumulator”, which starts with a value of init and the index of each element. reduceWithIndex returns the final value of the accumulator.

ocaml
  reduceWithIndex [|1;2;3;4|] 0 (fun acc x i -> acc + x + i) = 16;
reasonml
reduceWithIndex([|1, 2, 3, 4|], 0, (acc, x, i) => acc + x + i) == 16;
ocaml
val joinWithU : 'a t -> string -> ('a -> string) Js.Fn.arity1 -> string
reasonml
let joinWithU: t('a) => string => Js.Fn.arity1(('a => string)) => string;
ocaml
val joinWith : 'a t -> string -> ('a -> string) -> string
reasonml
let joinWith: t('a) => string => ('a => string) => string;

joinWith xs sep toString

Concatenates all the elements of xs converted to string with toString, each separated by sep, the string given as the second argument, into a single string. If the array has only one element, then that element will be returned without using the separator. If the array is empty, the empty string will be returned.

ocaml
  joinWith [|0; 1|] ", " string_of_int = "0, 1"
  joinWith [||] " " string_of_int = ""
  joinWith [|1|] " " string_of_int = "1"
reasonml
joinWith([|0, 1|], ", ", string_of_int)
== "0, 1"(joinWith, [||], " ", string_of_int)
== ""(joinWith, [|1|], " ", string_of_int)
== "1";
ocaml
val someU : 'a t -> ('a -> bool) Js.Fn.arity1 -> bool
reasonml
let someU: t('a) => Js.Fn.arity1(('a => bool)) => bool;
ocaml
val some : 'a t -> ('a -> bool) -> bool
reasonml
let some: t('a) => ('a => bool) => bool;

some xs p

returns true if at least one of the elements in xs satifies p;where p is a predicate: a function taking an element and returning a bool.

ocaml
  some [|2; 3; 4|] (fun x -> x mod 2 = 1) = true;;
  some [|-1; -3; -5|] (fun x -> x > 0) = false;;
reasonml
some([|2, 3, 4|], x => x mod 2 == 1) == true;
some([|(-1), (-3), (-5)|], x => x > 0) == false;
ocaml
val everyU : 'a t -> ('a -> bool) Js.Fn.arity1 -> bool
reasonml
let everyU: t('a) => Js.Fn.arity1(('a => bool)) => bool;
ocaml
val every : 'a t -> ('a -> bool) -> bool
reasonml
let every: t('a) => ('a => bool) => bool;

every xs p

returns true if all elements satisfy p;where p is a predicate: a function taking an element and returning a bool.

ocaml
  every [|1; 3; 5|] (fun x -> x mod 2 = 1) = true;;
  every [|1; -3; 5|] (fun x -> x > 0) = false;;
reasonml
every([|1, 3, 5|], x => x mod 2 == 1) == true;
every([|1, (-3), 5|], x => x > 0) == false;
ocaml
val every2U : 'a t -> 'b t -> ('a -> 'b -> bool) Js.Fn.arity2 -> bool
reasonml
let every2U: t('a) => t('b) => Js.Fn.arity2(('a => 'b => bool)) => bool;
ocaml
val every2 : 'a t -> 'b t -> ('a -> 'b -> bool) -> bool
reasonml
let every2: t('a) => t('b) => ('a => 'b => bool) => bool;

every2 xs ys p returns true if p xi yi is true for all pairs of elements up to the shorter length (i.e. min (length xs) (length ys))

ocaml
  every2 [|1;2;3|] [|0;1|] (>) = true;;
  every2 [||] [|1|] (fun  x y -> x > y) = true;;
  every2 [|2;3|] [|1|] (fun  x y -> x > y) = true;;
  every2 [|0;1|] [|5;0|] (fun x y -> x > y) = false;
reasonml
every2([|1, 2, 3|], [|0, 1|], (>)) == true;
every2([||], [|1|], (x, y) => x > y) == true;
every2([|2, 3|], [|1|], (x, y) => x > y) == true;
every2([|0, 1|], [|5, 0|], (x, y) => x > y) == false;
ocaml
val some2U : 'a t -> 'b t -> ('a -> 'b -> bool) Js.Fn.arity2 -> bool
reasonml
let some2U: t('a) => t('b) => Js.Fn.arity2(('a => 'b => bool)) => bool;
ocaml
val some2 : 'a t -> 'b t -> ('a -> 'b -> bool) -> bool
reasonml
let some2: t('a) => t('b) => ('a => 'b => bool) => bool;

some2 xs ys p returns true if p xi yi is true for any pair of elements up to the shorter length (i.e. min (length xs) (length ys))

ocaml
  some2 [|0;2|] [|1;0;3|] (>) = true ;;
  (some2 [||] [|1|] (fun   x y -> x > y)) =  false;;
  (some2 [|2;3|] [|1;4|] (fun   x y -> x > y)) = true;;
reasonml
some2([|0, 2|], [|1, 0, 3|], (>)) == true;
some2([||], [|1|], (x, y) => x > y) == false;
some2([|2, 3|], [|1, 4|], (x, y) => x > y) == true;
ocaml
val cmpU : 'a t -> 'a t -> ('a -> 'a -> int) Js.Fn.arity2 -> int
reasonml
let cmpU: t('a) => t('a) => Js.Fn.arity2(('a => 'a => int)) => int;
ocaml
val cmp : 'a t -> 'a t -> ('a -> 'a -> int) -> int
reasonml
let cmp: t('a) => t('a) => ('a => 'a => int) => int;

cmp xs ys f

  • Compared by length if length xs <> length ys;returning -1 iflength xs < length ys or 1 if length xs > length ys
  • Otherwise compare one by one f x y. f returns
  • a negative number if x is “less than” y
  • zero if x is “equal to” y
  • a positive number if x is “greater than” y
  • The comparison returns the first non-zero result of f;or zero if f returns zero for all x and y.
ocaml
  cmp [|1; 3; 5|] [|1; 4; 2|] (fun a b -> compare a b) = -1;;
  cmp [|1; 3; 5|] [|1; 2; 3|] (fun a b -> compare a b) = 1;;
  cmp [|1; 3; 5|] [|1; 3; 5|] (fun a b -> compare a b) = 0;;
reasonml
cmp([|1, 3, 5|], [|1, 4, 2|], (a, b) => compare(a, b)) == (-1);
cmp([|1, 3, 5|], [|1, 2, 3|], (a, b) => compare(a, b)) == 1;
cmp([|1, 3, 5|], [|1, 3, 5|], (a, b) => compare(a, b)) == 0;
ocaml
val eqU : 'a t -> 'a t -> ('a -> 'a -> bool) Js.Fn.arity2 -> bool
reasonml
let eqU: t('a) => t('a) => Js.Fn.arity2(('a => 'a => bool)) => bool;
ocaml
val eq : 'a t -> 'a t -> ('a -> 'a -> bool) -> bool
reasonml
let eq: t('a) => t('a) => ('a => 'a => bool) => bool;

eq xs ys

  • return false if length is not the same
  • otherwise compare items one by one using f xi yi;and return true if all results are true;false otherwise
ocaml
  eq [|1; 2; 3|] [|-1; -2; -3|] (fun a b -> abs a = abs b) = true
reasonml
eq([|1, 2, 3|], [|(-1), (-2), (-3)|], (a, b) => abs(a) == abs(b)) == true;
ocaml
val truncateToLengthUnsafe : 'a t -> int -> unit
reasonml
let truncateToLengthUnsafe: t('a) => int => unit;

Unsafe truncateToLengthUnsafe xs n sets length of array xs to n.

If n is greater than the length of xs;the extra elements are set to Js.Null_undefined.null

If n is less than zero;raises a RangeError.

ocaml
  let arr = [|"ant";"bee";"cat";"dog";"elk"|];;
  let () = truncateToLengthUnsafe arr 3;;
  arr = [|"ant";"bee";"cat"|];;
reasonml
let arr = [|"ant", "bee", "cat", "dog", "elk"|];
let () = truncateToLengthUnsafe(arr, 3);
arr == [|"ant", "bee", "cat"|];
ocaml
val initU : int -> (int -> 'a) Js.Fn.arity1 -> 'a t
reasonml
let initU: int => Js.Fn.arity1((int => 'a)) => t('a);
ocaml
val init : int -> (int -> 'a) -> 'a t
reasonml
let init: int => (int => 'a) => t('a);
ocaml
val push : 'a t -> 'a -> unit
reasonml
let push: t('a) => 'a => unit;

arr->push(item) push element `item` into the array