Array
fromArrayLike
let fromArrayLike: Js.Array2.array_like<'a> => array<'a>
fromArrayLikeWithMap
let fromArrayLikeWithMap: (Js.Array2.array_like<'a>, 'a => 'b) => array<'b>
make
let make: (~length: int, 'a) => array<'a>
make(~length, init)
Creates an array of length length
initialized with the value of init
.
RESArray.make(~length=3, #apple) == [#apple, #apple, #apple]
fromInitializer
let fromInitializer: (~length: int, int => 'a) => array<'a>
fromInitializer(~length, f)
Creates an array of length length
initialized with the value returned from f
for each index.
RESArray.make(~length=3, i => i + 3) == [3, 4, 5]
equal
let equal: (array<'a>, array<'a>, ('a, 'a) => bool) => bool
compare
let compare: (
array<'a>,
array<'a>,
('a, 'a) => Core__Ordering.t,
) => Core__Ordering.t
isArray
let isArray: 'a => bool
length
let length: array<'a> => int
length(array)
returns the length of (i.e. number of items in) the array.
See Array.length
on MDN.
Examples
RESCRIPTlet someArray = ["hi", "hello"]
Console.log(someArray->Array.length) // 2
copyAllWithin
let copyAllWithin: (array<'a>, ~target: int) => array<'a>
copyWithinToEnd
let copyWithinToEnd: (array<'a>, ~target: int, ~start: int) => array<'a>
copyWithin
let copyWithin: (
array<'a>,
~target: int,
~start: int,
~end: int,
) => array<'a>
fillAll
let fillAll: (array<'a>, 'a) => unit
fillAll(array, value)
fills the entire array
with value
.
Beware this will mutate the array.
See Array.fill
on MDN.
Examples
RESCRIPTlet myArray = [1, 2, 3, 4]
myArray->Array.fillAll(9)
Console.log(myArray) // [9, 9, 9, 9]
fillToEnd
let fillToEnd: (array<'a>, 'a, ~start: int) => unit
fillToEnd(array, value, ~start)
fills array
with value
from the start
index.
Beware this will mutate the array.
See Array.fill
on MDN.
Examples
RESCRIPTlet myArray = [1, 2, 3, 4]
myArray->Array.fillToEnd(9, ~start=1)
Console.log(myArray) // [1, 9, 9, 9]
fill
let fill: (array<'a>, 'a, ~start: int, ~end: int) => unit
fill(array, value, ~start, ~end)
fills array
with value
from start
to end
.
Beware this will mutate the array.
See Array.fill
on MDN.
Examples
RESCRIPTlet myArray = [1, 2, 3, 4]
myArray->Array.fill(9, ~start=1, ~end=2)
Console.log(myArray) // [1, 9, 9, 4]
pop
let pop: array<'a> => option<'a>
pop(array)
removes the last item from array
and returns it.
Beware this will mutate the array.
See Array.pop
on MDN.
Examples
RESCRIPTlet someArray = ["hi", "hello"]
let lastItem = someArray->Array.pop // "hello"
Console.log(someArray) // ["hi"]. Notice last item is gone.
push
let push: (array<'a>, 'a) => unit
push(array, item)
appends item
to the end of array
.
Beware this will mutate the array.
See Array.push
on MDN.
Examples
RESCRIPTlet someArray = ["hi", "hello"]
someArray->Array.push("yay")
Console.log(someArray) // ["hi", "hello", "yay"]
pushMany
let pushMany: (array<'a>, array<'a>) => unit
pushMany(array, itemsArray)
appends many new items to the end of the array.
Beware this will mutate the array.
See Array.push
on MDN.
Examples
RESCRIPTlet someArray = ["hi", "hello"]
someArray->Array.pushMany(["yay", "wehoo"])
Console.log(someArray) // ["hi", "hello", "yay", "wehoo"]
reverse
let reverse: array<'a> => unit
reverse(array)
reverses the order of the items in array
.
Beware this will mutate the array.
See Array.reverse
on MDN.
Examples
RESCRIPTlet someArray = ["hi", "hello"]
someArray->Array.reverse
Console.log(someArray) // ["hello", "h1"]
shift
let shift: array<'a> => option<'a>
shift(array)
removes the first item in the array, and returns it.
Beware this will mutate the array.
See Array.shift
on MDN.
Examples
RESCRIPTlet someArray = ["hi", "hello"]
let lastItem = someArray->Array.shift // "hi"
Console.log(someArray) // ["hello"]. Notice first item is gone.
toSorted
let toSorted: (array<'a>, ('a, 'a) => Core__Ordering.t) => array<'a>
toSorted(array, comparator)
returns a new, sorted array from array
, using the comparator
function.
See Array.toSorted
on MDN.
Examples
RESCRIPTlet someArray = [3, 2, 1]
let sorted = someArray->Array.toSorted(Int.compare)
Console.log(sorted) // [1, 2, 3]
Console.log(someArray) // [3, 2, 1]. Original unchanged
sort
let sort: (array<'a>, ('a, 'a) => Core__Ordering.t) => unit
sort(array, comparator)
sorts array
in-place using the comparator
function.
Beware this will mutate the array.
See Array.sort
on MDN.
Examples
RESCRIPTlet someArray = [3, 2, 1]
someArray->Array.sort((a, b) => float(a - b))
Console.log(someArray) // [1, 2, 3]
splice
let splice: (
array<'a>,
~start: int,
~remove: int,
~insert: array<'a>,
) => unit
toSpliced
let toSpliced: (
array<'a>,
~start: int,
~remove: int,
~insert: array<'a>,
) => array<'a>
with
let with: (array<'a>, int, 'a) => array<'a>
unshift
let unshift: (array<'a>, 'a) => unit
unshift(array, item)
inserts a new item at the start of the array.
Beware this will mutate the array.
See Array.unshift
on MDN.
Examples
RESCRIPTlet someArray = ["hi", "hello"]
someArray->Array.unshift("yay")
Console.log(someArray) // ["yay", "hi", "hello"]
unshiftMany
let unshiftMany: (array<'a>, array<'a>) => unit
unshiftMany(array, itemsArray)
inserts many new items to the start of the array.
Beware this will mutate the array.
See Array.push
on MDN.
Examples
RESCRIPTlet someArray = ["hi", "hello"]
someArray->Array.unshiftMany(["yay", "wehoo"])
Console.log(someArray) // ["yay", "wehoo", "hi", "hello"]
concat
let concat: (array<'a>, array<'a>) => array<'a>
concat(array1, array2)
concatenates the two arrays, creating a new array.
See Array.concat
on MDN.
Examples
RESCRIPTlet array1 = ["hi", "hello"]
let array2 = ["yay", "wehoo"]
let someArray = array1->Array.concat(array2)
Console.log(someArray) // ["hi", "hello", "yay", "wehoo"]
concatMany
let concatMany: (array<'a>, array<array<'a>>) => array<'a>
concatMany(array1, arrays)
concatenates array1 with several other arrays, creating a new array.
See Array.concat
on MDN.
Examples
RESCRIPTlet array1 = ["hi", "hello"]
let array2 = ["yay"]
let array3 = ["wehoo"]
let someArray = array1->Array.concatMany([array2, array3])
Console.log(someArray) // ["hi", "hello", "yay", "wehoo"]
flat
let flat: array<array<'a>> => array<'a>
flat(arrays)
concatenates an array of arrays into a single array.
See Array.flat
on MDN.
Examples
RESCRIPTConsole.log([[1], [2], [3, 4]]->Array.flat) // [1, 2, 3, 4]
includes
let includes: (array<'a>, 'a) => bool
includes(array, item)
checks whether array
includes item
, by doing a strict check for equality.
See Array.includes
on MDN.
Examples
RESCRIPTConsole.log([1, 2]->Array.includes(1)) // true
Console.log([1, 2]->Array.includes(3)) // false
Console.log([{"language": "ReScript"}]->Array.includes({"language": "ReScript"})) // false, because of strict equality
indexOf
let indexOf: (array<'a>, 'a) => int
indexOf(array, item)
returns the index of the provided item
in array
. Uses strict check for equality when comparing items.
Returns -1
if the item doesn not exist. Check out Array.indexOfOpt
for a version that returns None
instead of -1
if the item does not exist.
See Array.indexOf
on MDN.
Examples
RESCRIPTConsole.log([1, 2]->Array.indexOf(2)) // 1
Console.log([1, 2]->Array.indexOf(3)) // -1
Console.log([{"language": "ReScript"}]->Array.indexOf({"language": "ReScript"})) // -1, because of strict equality
indexOfOpt
let indexOfOpt: (array<'a>, 'a) => option<int>
indexOfOpt(array, item)
returns an option of the index of the provided item
in array
. Uses strict check for equality when comparing items.
See Array.indexOf
on MDN.
Examples
RESCRIPTConsole.log([1, 2]->Array.indexOfOpt(2)) // Some(1)
Console.log([1, 2]->Array.indexOfOpt(3)) // None
Console.log([{"language": "ReScript"}]->Array.indexOfOpt({"language": "ReScript"})) // None, because of strict equality
indexOfFrom
let indexOfFrom: (array<'a>, 'a, int) => int
joinWith
let joinWith: (array<string>, string) => string
joinWith(array, separator)
produces a string where all items of array
are printed, separated by separator
. Array items must be strings, to join number or other arrays, use joinWithUnsafe
. Under the hood this will run JavaScript's toString
on all the array items.
Examples
RESCRIPTlet array = ["One", "Two", "Three"]
Console.log(array->Array.joinWith(" -- ")) // One -- Two -- Three
joinWithUnsafe
let joinWithUnsafe: (array<'a>, string) => string
joinWithUnsafe(array, separator)
produces a string where all items of array
are printed, separated by separator
. Under the hood this will run JavaScript's toString
on all the array items.
Examples
RESCRIPTlet array = [1, 2, 3]
Console.log(array->Array.joinWithUnsafe(" -- ")) // 1 -- 2 -- 3
lastIndexOf
let lastIndexOf: (array<'a>, 'a) => int
lastIndexOfOpt
let lastIndexOfOpt: (array<'a>, 'a) => option<int>
lastIndexOfFrom
let lastIndexOfFrom: (array<'a>, 'a, int) => int
slice
let slice: (array<'a>, ~start: int, ~end: int) => array<'a>
slice(array, ~start, ~end)
creates a new array of items copied from array
from start
until (but not including) end
.
See Array.slice
on MDN.
Examples
RESCRIPTlet myArray = [1, 2, 3, 4]
Console.log(myArray->Array.slice(~start=1, ~end=3)) // [2, 3]
sliceToEnd
let sliceToEnd: (array<'a>, ~start: int) => array<'a>
sliceToEnd(array, start)
creates a new array from array
, with all items from array
starting from start
.
See Array.slice
on MDN.
Examples
RESCRIPTlet myArray = [1, 2, 3, 4]
Console.log(myArray->Array.sliceToEnd(~start=1)) // [2, 3, 4]
copy
let copy: array<'a> => array<'a>
copy(array)
makes a copy of the array with the items in it, but does not make copies of the items themselves.
Examples
RESCRIPTlet myArray = [1, 2, 3]
let copyOfMyArray = myArray->Array.copy
Console.log(copyOfMyArray) // [1, 2, 3]
Console.log(myArray === copyOfMyArray) // false
toString
let toString: array<'a> => string
toString(array)
stringifies array
by running toString
on all of the array elements and joining them with ",".
See Array.toString
on MDN.
Examples
RESCRIPTlet array = [1, 2, 3, 4]
Console.log(array->Array.toString) // "1,2,3,4"
toLocaleString
let toLocaleString: array<'a> => string
every
let every: (array<'a>, 'a => bool) => bool
every(array, predicate)
returns true if predicate
returns true for all items in array
.
See Array.every
on MDN.
Examples
RESCRIPTlet array = [1, 2, 3, 4]
Console.log(array->Array.every(num => num > 4)) // true
Console.log(array->Array.every(num => num === 1)) // false
everyWithIndex
let everyWithIndex: (array<'a>, ('a, int) => bool) => bool
everyWithIndex(array, checker)
returns true if all items in array
returns true when running the provided checker
function.
See Array.every
on MDN.
Examples
RESCRIPTlet array = [1, 2, 3, 4]
Console.log(array->Array.everyWithIndex((num, index) => index < 2 && num <= 2)) // true
Console.log(array->Array.everyWithIndex((num, index) => index < 2 && num >= 2)) // false
filter
let filter: (array<'a>, 'a => bool) => array<'a>
filter(array, checker)
returns a new array containing all elements from array
for which the provided checker
function returns true.
See Array.filter
on MDN.
Examples
RESCRIPTlet array = [1, 2, 3, 4]
Console.log(array->Array.filter(num => num > 2)) // [3, 4]
filterWithIndex
let filterWithIndex: (array<'a>, ('a, int) => bool) => array<'a>
filterWithIndex(array, checker)
returns a new array containing all elements from array
for which the provided checker
function returns true.
See Array.filter
on MDN.
Examples
RESCRIPTlet array = [1, 2, 3, 4]
Console.log(array->Array.filterWithIndex((num, index) => index === 0 || num === 2)) // [1, 2]
find
let find: (array<'a>, 'a => bool) => option<'a>
find(array, checker)
returns the first element of array
where the provided checker
function returns true.
See Array.find
on MDN.
Examples
RESCRIPTtype languages = ReScript | TypeScript | JavaScript
let array = [ReScript, TypeScript, JavaScript]
switch array->Array.find(item => item == ReScript) {
| None => Console.log("No item...")
| Some(_) => Console.log("Yay, ReScript!")
}
findWithIndex
let findWithIndex: (array<'a>, ('a, int) => bool) => option<'a>
findWithIndex(array, checker)
returns the first element of array
where the provided checker
function returns true.
See Array.find
on MDN.
Examples
RESCRIPTtype languages = ReScript | TypeScript | JavaScript
let array = [TypeScript, JavaScript, ReScript]
switch array->Array.findWithIndex((item, index) => index > 1 && item == ReScript) {
| None => Console.log("No item...")
| Some(_) => Console.log("Yay, ReScript exists in a later position!")
}
findIndex
let findIndex: (array<'a>, 'a => bool) => int
findIndex(array, checker)
returns the index of the first element of array
where the provided checker
function returns true.
Returns -1
if the item does not exist. Consider using Array.findIndexOpt
if you want an option instead (where -1
would be None
).
See Array.findIndex
on MDN.
Examples
RESCRIPTtype languages = ReScript | TypeScript | JavaScript
let array = [ReScript, JavaScript]
Console.log(array->Array.findIndex(item => item == ReScript)) // 0
Console.log(array->Array.findIndex(item => item == TypeScript)) // -1
findIndexWithIndex
let findIndexWithIndex: (array<'a>, ('a, int) => bool) => int
findIndexWithIndex(array, checker)
returns the index of the first element of array
where the provided checker
function returns true.
Returns -1
if the item does not exist. Consider using Array.findIndexOpt
if you want an option instead (where -1
would be None
).
See Array.findIndex
on MDN.
Examples
RESCRIPTtype languages = ReScript | TypeScript | JavaScript
let array = [ReScript, JavaScript]
Console.log(array->Array.findIndexWithIndex((item, index) => index === 0 && item == ReScript)) // 0
Console.log(array->Array.findIndex((item, index) => index === 0 && item == TypeScript)) // -1
forEach
let forEach: (array<'a>, 'a => unit) => unit
forEach(array, fn)
runs the provided fn
on every element of array
.
See Array.forEach
on MDN.
Examples
RESCRIPTlet array = ["Hello", "Hi", "Good bye"]
array->Array.forEach(item => {
Console.log(item)
})
forEachWithIndex
let forEachWithIndex: (array<'a>, ('a, int) => unit) => unit
forEachWithIndex(array, fn)
runs the provided fn
on every element of array
.
See Array.forEach
on MDN.
Examples
RESCRIPTlet array = ["Hello", "Hi", "Good bye"]
array->Array.forEachWithIndex((item, index) => {
Console.log("At item " ++ Int.toString(index) ++ ": " ++ item)
})
map
let map: (array<'a>, 'a => 'b) => array<'b>
map(array, fn)
returns a new array with all elements from array
, each element transformed using the provided fn
.
See Array.map
on MDN.
Examples
RESCRIPTlet array = ["Hello", "Hi", "Good bye"]
let mappedArray = array->Array.map(greeting => greeting ++ " to you")
Console.log(mappedArray) // ["Hello to you", "Hi to you", "Good bye to you"]
mapWithIndex
let mapWithIndex: (array<'a>, ('a, int) => 'b) => array<'b>
mapWithIndex(array, fn)
returns a new array with all elements from array
, each element transformed using the provided fn
.
See Array.map
on MDN.
Examples
RESCRIPTlet array = ["Hello", "Hi", "Good bye"]
let mappedArray =
array->Array.mapWithIndex((greeting, index) =>
greeting ++ " at position " ++ Int.toString(index)
)
Console.log(mappedArray) // ["Hello at position 0", "Hi at position 1", "Good bye at position 2"]
reduce
let reduce: (array<'a>, 'b, ('b, 'a) => 'b) => 'b
reduce(xs, f, init)
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.
RESArray.reduce([2, 3, 4], (a, b) => a + b, 1) == 10
Array.reduce(["a", "b", "c", "d"], (a, b) => a ++ b, "") == "abcd"
reduceWithIndex
let reduceWithIndex: (array<'a>, 'b, ('b, 'a, int) => 'b) => 'b
reduceWithIndex(xs, f, init)
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.
RESArray.reduceWithIndex([1, 2, 3, 4], (acc, x, i) => acc + x + i, 0) == 16
reduceRight
let reduceRight: (array<'a>, 'b, ('b, 'a) => 'b) => 'b
reduceRight(xs, f, init)
Works like Array.reduce
; except that function f
is applied to each item of xs
from the last back to the first.
RESArray.reduceRight(["a", "b", "c", "d"], (a, b) => a ++ b, "") == "dcba"
reduceRightWithIndex
let reduceRightWithIndex: (array<'a>, 'b, ('b, 'a, int) => 'b) => 'b
reduceRightWithIndex(xs, f, init)
Like reduceRight
, but with an additional index argument on the callback function.
RESArray.reduceRightWithIndex([1, 2, 3, 4], (acc, x, i) => acc + x + i, 0) == 16
some
let some: (array<'a>, 'a => bool) => bool
some(array, predicate)
returns true if predicate
returns true for any element in array
.
See Array.some
on MDN.
Examples
RESCRIPTlet array = ["Hello", "Hi", "Good bye"]
Console.log(array->Array.some(greeting => greeting === "Hello")) // true
someWithIndex
let someWithIndex: (array<'a>, ('a, int) => bool) => bool
someWithIndex(array, checker)
returns true if running the provided checker
function on any element in array
returns true.
See Array.some
on MDN.
Examples
RESCRIPTlet array = ["Hello", "Hi", "Good bye"]
Console.log(array->Array.someWithIndex((greeting, index) => greeting === "Hello" && index === 0)) // true
get
let get: (array<'a>, int) => option<'a>
get(array, index)
returns the element at index
of array
.
Returns None
if the index does not exist in the array. Equivalent to doing array[index]
in JavaScript.
Examples
RESCRIPTlet array = ["Hello", "Hi", "Good bye"]
array->Array.get(0) == Some("Hello") // true
set
let set: (array<'a>, int, 'a) => unit
set(array, index, item)
sets the provided item
at index
of array
.
Beware this will mutate the array.
Examples
RESCRIPTlet array = ["Hello", "Hi", "Good bye"]
array->Array.set(1, "Hello")
Console.log(array[1]) // "Hello"
getSymbol
let getSymbol: (array<'a>, Core__Symbol.t) => option<'b>
getSymbolUnsafe
let getSymbolUnsafe: (array<'a>, Core__Symbol.t) => 'b
setSymbol
let setSymbol: (array<'a>, Core__Symbol.t, 'b) => unit
getUnsafe
let getUnsafe: (array<'a>, int) => 'a
getUnsafe(array, index)
returns the element at index
of array
.
This is unsafe, meaning it will fail with an exception if index
does not exist in array
.
Exceptions
Not_found
: If the providedindex
does not exist inarray
.
Examples
RESCRIPTlet array = ["Hello", "Hi", "Good bye"]
Console.log(array->Array.getUnsafe(0)) // "Hello"
Console.log(array->Array.getUnsafe(3)) // Fails and raises exception
setUnsafe
let setUnsafe: (array<'a>, int, 'a) => unit
setUnsafe(array, index, item)
sets the provided item
at index
of array
.
Beware this will mutate the array, and is unsafe.
Examples
RESCRIPTlet array = ["Hello", "Hi", "Good bye"]
array->Array.setUnsafe(1, "Hello")
Console.log(array[1]) // "Hello"
findIndexOpt
let findIndexOpt: (array<'a>, 'a => bool) => option<int>
findIndexOpt(array, checker)
returns the index of the first element of array
where the provided checker
function returns true.
Returns None
if no item matches.
See Array.findIndex
on MDN.
Examples
RESCRIPTtype languages = ReScript | TypeScript | JavaScript
let array = [ReScript, TypeScript, JavaScript]
switch array->Array.findIndexOpt(item => item == ReScript) {
| None => Console.log("Ahh, no ReScript...")
| Some(index) => Console.log("Yay, ReScript at index " ++ Int.toString(index))
}
toReversed
let toReversed: array<'a> => array<'a>
toReversed(array)
creates a new array with all items from array
in reversed order.
See Array.toReversed
on MDN.
Examples
RESCRIPTlet someArray = ["hi", "hello"]
let reversed = someArray->Array.toReversed
Console.log(reversed) // ["hello", "h1"]
Console.log(someArray) // ["h1", "hello"]. Original unchanged
filterMap
let filterMap: (array<'a>, 'a => option<'b>) => array<'b>
get(array, index)
returns the element at index
of array
.
Returns None
if the index does not exist in the array. Equivalent to doing array[index]
in JavaScript.
Examples
RESCRIPTlet array = ["Hello", "Hi", "Good bye"]
Console.log(
array->Array.filterMap(item =>
switch item {
| "Hello" => Some(item->String.length)
| _ => None
}
),
)
// [5]
keepSome
let keepSome: array<option<'a>> => array<'a>
keepSome(arr)
Returns a new array containing value
for all elements that are Some(value)
and ignoring every value that is None
RESArray.keepSome([Some(1), None, Some(3)]) == [1, 3]
toShuffled
let toShuffled: array<'a> => array<'a>
toShuffled(array)
returns a new array with all items in array
in a random order.
Examples
RESCRIPTlet array = ["Hello", "Hi", "Good bye"]
let shuffledArray = array->Array.toShuffled
Console.log(shuffledArray)
shuffle
let shuffle: array<'a> => unit
shuffle(array)
randomizes the position of all items in array
.
Beware this will mutate the array.
Examples
RESCRIPTlet array = ["Hello", "Hi", "Good bye"]
array->Array.shuffle
Console.log(array)
flatMap
let flatMap: (array<'a>, 'a => array<'b>) => array<'b>
flatMap(array, mapper)
returns a new array concatenating the arrays returned from running mapper
on all items in array
.
Examples
RESCRIPTtype language = ReScript | TypeScript | JavaScript
let array = [ReScript, TypeScript, JavaScript]
Console.log(
array->Array.flatMap(item =>
switch item {
| ReScript => [1, 2, 3]
| TypeScript => [4, 5, 6]
| JavaScript => [7, 8, 9]
}
),
)
// [1, 2, 3, 4, 5, 6, 7, 8, 9]
findMap
let findMap: (array<'a>, 'a => option<'b>) => option<'b>
findMap(arr, f)
Calls f
for each element and returns the first value from f
that is Some(_)
.
Otherwise returns None
RESArray.findMap([1, 2, 3], n => mod(n, 2) ? Some(n - 2) : None) == 0
at
let at: (array<'a>, int) => option<'a>
at(array, index)
Get an element by its index. Negative indices count backwards from the last item.
Examples
RESCRIPT["a", "b", "c"]->Array.at(0) // Some("a")
["a", "b", "c"]->Array.at(2) // Some("c")
["a", "b", "c"]->Array.at(3) // None
["a", "b", "c"]->Array.at(-1) // Some("c")
["a", "b", "c"]->Array.at(-3) // Some("a")
["a", "b", "c"]->Array.at(-4) // None