Array
Provides bindings to JavaScript’s Array
functions. These bindings are
optimized for pipe-last (|>
), where the array to be processed is the last
parameter in the function.
Here is an example to find the sum of squares of all even numbers in an array. Without pipe last, we must call the functions in reverse order:
Examples
RESCRIPTlet isEven = x => mod(x, 2) == 0
let square = x => x * x
let result = {
open Js.Array
reduce(\"+", 0, map(square, filter(isEven, [5, 2, 3, 4, 1])))
}
With pipe last, we call the functions in the “natural” order:
RESCRIPTlet isEven = x => mod(x, 2) == 0
let square = x => x * x
let result = {
open Js.Array
[5, 2, 3, 4, 1] |> filter(isEven) |> map(square) |> reduce("+", 0)
}
t
type t<'a> = array<'a>
The type used to describe a JavaScript array.
array_like
type array_like<'a> = Js_array2.array_like<'a>
A type used to describe JavaScript objects that are like an array or are iterable.
from
let from: array_like<'a> => array<'a>
Creates a shallow copy of an array from an array-like object. See Array.from
on MDN.
Examples
RESCRIPTlet strArr = Js.String.castToArrayLike("abcd")
Js.Array.from(strArr) == ["a", "b", "c", "d"]
fromMap
let fromMap: (array_like<'a>, 'a => 'b) => array<'b>
Creates a new array by applying a function (the second argument) to each item
in the array_like
first argument. See
Array.from
on MDN.
Examples
RESCRIPTlet strArr = Js.String.castToArrayLike("abcd")
let code = s => Js.String.charCodeAt(0, s)
Js.Array.fromMap(strArr, code) == [97.0, 98.0, 99.0, 100.0]
isArray
let isArray: 'a => bool
length
let length: array<'a> => int
Returns the number of elements in the array. See Array.length
on MDN.
copyWithin
let copyWithin: (~to_: int, t<'a>) => t<'a>
copyWithinFrom
let copyWithinFrom: (~to_: int, ~from: int, t<'a>) => t<'a>
copyWithinFromRange
let copyWithinFromRange: (~to_: int, ~start: int, ~end_: int, t<'a>) => t<'a>
fillInPlace
let fillInPlace: ('a, t<'a>) => t<'a>
fillFromInPlace
let fillFromInPlace: ('a, ~from: int, t<'a>) => t<'a>
fillRangeInPlace
let fillRangeInPlace: ('a, ~start: int, ~end_: int, t<'a>) => t<'a>
pop
let pop: t<'a> => option<'a>
If the array is not empty, removes the last element and returns it as Some(value)
; returns None
if the array is empty. This function modifies the original array. See Array.pop
on MDN.
Examples
RESCRIPTlet arr = [100, 101, 102, 103, 104]
Js.Array.pop(arr) == Some(104)
arr == [100, 101, 102, 103]
let empty: array<int> = []
Js.Array.pop(empty) == None
push
let push: ('a, t<'a>) => int
pushMany
let pushMany: (array<'a>, t<'a>) => int
reverseInPlace
let reverseInPlace: t<'a> => t<'a>
Returns an array with the elements of the input array in reverse order. This function modifies the original array. See Array.reverse
on MDN.
Examples
RESCRIPTlet arr = ["ant", "bee", "cat"]
Js.Array.reverseInPlace(arr) == ["cat", "bee", "ant"]
arr == ["cat", "bee", "ant"]
shift
let shift: t<'a> => option<'a>
If the array is not empty, removes the first element and returns it as Some(value)
; returns None
if the array is empty. This function modifies the original array. See Array.shift
on MDN.
Examples
RESCRIPTlet arr = [100, 101, 102, 103, 104]
Js.Array.shift(arr) == Some(100)
arr == [101, 102, 103, 104]
let empty: array<int> = []
Js.Array.shift(empty) == None
sortInPlace
let sortInPlace: t<'a> => t<'a>
Sorts the given array in place and returns the sorted array. JavaScript sorts the array by converting the arguments to UTF-16 strings and sorting them. See the second example with sorting numbers, which does not do a numeric sort. This function modifies the original array. See Array.sort
on MDN.
Examples
RESCRIPTlet words = ["bee", "dog", "ant", "cat"]
Js.Array.sortInPlace(words) == ["ant", "bee", "cat", "dog"]
words == ["ant", "bee", "cat", "dog"]
let numbers = [3, 30, 10, 1, 20, 2]
Js.Array.sortInPlace(numbers) == [1, 10, 2, 20, 3, 30]
numbers == [1, 10, 2, 20, 3, 30]
sortInPlaceWith
let sortInPlaceWith: (('a, 'a) => int, t<'a>) => t<'a>
spliceInPlace
let spliceInPlace: (~pos: int, ~remove: int, ~add: array<'a>, t<'a>) => t<'a>
removeFromInPlace
let removeFromInPlace: (~pos: int, t<'a>) => t<'a>
removeCountInPlace
let removeCountInPlace: (~pos: int, ~count: int, t<'a>) => t<'a>
unshift
let unshift: ('a, t<'a>) => int
unshiftMany
let unshiftMany: (array<'a>, t<'a>) => int
concat
let concat: (t<'a>, t<'a>) => t<'a>
concatMany
let concatMany: (array<t<'a>>, t<'a>) => t<'a>
includes
let includes: ('a, t<'a>) => bool
indexOf
let indexOf: ('a, t<'a>) => int
indexOfFrom
let indexOfFrom: ('a, ~from: int, t<'a>) => int
join
Deprecated
please use joinWith instead
let join: t<'a> => string
joinWith
let joinWith: (string, t<'a>) => string
lastIndexOf
let lastIndexOf: ('a, t<'a>) => int
lastIndexOfFrom
let lastIndexOfFrom: ('a, ~from: int, t<'a>) => int
slice
let slice: (~start: int, ~end_: int, t<'a>) => t<'a>
copy
let copy: t<'a> => t<'a>
Returns a copy of the entire array. Same as Js.Array.Slice(~start=0, ~end_=Js.Array.length(arr), arr)
. See
Array.slice
on MDN.
sliceFrom
let sliceFrom: (int, t<'a>) => t<'a>
toString
let toString: t<'a> => string
Converts the array to a string. Each element is converted to a string using
JavaScript. Unlike the JavaScript Array.toString()
, all elements in a
ReasonML array must have the same type. See
Array.toString
on MDN.
Examples
RESCRIPTJs.Array.toString([3.5, 4.6, 7.8]) == "3.5,4.6,7.8"
Js.Array.toString(["a", "b", "c"]) == "a,b,c"
toLocaleString
let toLocaleString: t<'a> => string
Converts the array to a string using the conventions of the current locale.
Each element is converted to a string using JavaScript. Unlike the JavaScript
Array.toLocaleString()
, all elements in a ReasonML array must have the same
type. See
Array.toLocaleString
on MDN.
Examples
RESCRIPTJs.Array.toLocaleString([Js.Date.make()])
// returns "3/19/2020, 10:52:11 AM" for locale en_US.utf8
// returns "2020-3-19 10:52:11" for locale de_DE.utf8
every
let every: ('a => bool, t<'a>) => bool
everyi
let everyi: (('a, int) => bool, t<'a>) => bool
filter
let filter: ('a => bool, t<'a>) => t<'a>
filteri
let filteri: (('a, int) => bool, t<'a>) => t<'a>
find
let find: ('a => bool, t<'a>) => option<'a>
findi
let findi: (('a, int) => bool, t<'a>) => option<'a>
findIndex
let findIndex: ('a => bool, t<'a>) => int
findIndexi
let findIndexi: (('a, int) => bool, t<'a>) => int
forEach
let forEach: ('a => unit, t<'a>) => unit
forEachi
let forEachi: (('a, int) => unit, t<'a>) => unit
map
let map: ('a => 'b, t<'a>) => t<'b>
mapi
let mapi: (('a, int) => 'b, t<'a>) => t<'b>
reduce
let reduce: (('a, 'b) => 'a, 'a, t<'b>) => 'a
reducei
let reducei: (('a, 'b, int) => 'a, 'a, t<'b>) => 'a
reduceRight
let reduceRight: (('a, 'b) => 'a, 'a, t<'b>) => 'a
reduceRighti
let reduceRighti: (('a, 'b, int) => 'a, 'a, t<'b>) => 'a
some
let some: ('a => bool, t<'a>) => bool
somei
let somei: (('a, int) => bool, t<'a>) => bool
unsafe_get
let unsafe_get: (array<'a>, int) => 'a
Returns the value at the given position in the array if the position is in
bounds; returns the JavaScript value undefined
otherwise.
Examples
RESCRIPTlet arr = [100, 101, 102, 103]
Js.Array.unsafe_get(arr, 3) == 103
Js.Array.unsafe_get(arr, 4) // returns undefined
unsafe_set
let unsafe_set: (array<'a>, int, 'a) => unit
Sets the value at the given position in the array if the position is in bounds. If the index is out of bounds, well, “here there be dragons.“ This function modifies the original array.
Examples
RESCRIPTlet arr = [100, 101, 102, 103]
Js.Array.unsafe_set(arr, 3, 99)
// result is [100, 101, 102, 99]
Js.Array.unsafe_set(arr, 4, 88)
// result is [100, 101, 102, 99, 88]
Js.Array.unsafe_set(arr, 6, 77)
// result is [100, 101, 102, 99, 88, <1 empty item>, 77]
Js.Array.unsafe_set(arr, -1, 66)
// you don't want to know.