Array
A mutable array.
Compiles to a regular JavaScript array.
t
type t<'a> = array<'a>
Type representing an array of value 'a
.
arrayLike
type arrayLike<'a>
fromIterator
let fromIterator: Iterator.t<'a> => array<'a>
fromIterator(iterator)
creates an array from the provided iterator
Examples
RESCRIPTMap.fromArray([("foo", 1), ("bar", 2)])
->Map.values
->Array.fromIterator == [1, 2]
fromArrayLike
let fromArrayLike: arrayLike<'a> => array<'a>
fromArrayLikeWithMap
let fromArrayLikeWithMap: (arrayLike<'a>, 'a => 'b) => array<'b>
fromString
let fromString: string => array<string>
fromString(str)
creates an array of each character as a separate string from the provided str
.
Examples
RESCRIPTArray.fromString("abcde") == ["a", "b", "c", "d", "e"]
make
let make: (~length: int, 'a) => array<'a>
make(~length, init)
creates an array of length length
initialized with the value of init
.
Examples
RESCRIPTArray.make(~length=3, #apple) == [#apple, #apple, #apple]
Array.make(~length=6, 7) == [7, 7, 7, 7, 7, 7]
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.
Examples
RESCRIPTArray.fromInitializer(~length=3, i => i + 3) == [3, 4, 5]
Array.fromInitializer(~length=7, i => i + 3) == [3, 4, 5, 6, 7, 8, 9]
equal
let equal: (array<'a>, array<'a>, ('a, 'a) => bool) => bool
compare
let compare: (array<'a>, array<'a>, ('a, 'a) => Ordering.t) => 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"]
someArray->Array.length == 2
isEmpty
let isEmpty: array<'a> => bool
isEmpty(array)
returns true
if the array is empty (has length 0), false
otherwise.
Examples
RESCRIPT[]->Array.isEmpty->assertEqual(true)
[1, 2, 3]->Array.isEmpty->assertEqual(false)
let emptyArray = []
emptyArray->Array.isEmpty->assertEqual(true)
let nonEmptyArray = ["hello"]
nonEmptyArray->Array.isEmpty->assertEqual(false)
copyAllWithin
Deprecated
let copyAllWithin: (array<'a>, ~target: int) => array<'a>
isEmpty(array)
returns true
if the array is empty (has length 0), false
otherwise.
Examples
RESCRIPTlet arr = [100, 101, 102, 103, 104]
arr->Array.copyAllWithin(~target=2) == [100, 101, 100, 101, 102]
arr == [100, 101, 100, 101, 102]
copyWithinToEnd
Deprecated
let copyWithinToEnd: (array<'a>, ~target: int, ~start: int) => array<'a>
copyWithinToEnd(array, ~target, ~start)
copies starting at element start
in the given array to the designated target
position, returning the resulting array.
Beware this will mutate the array.
See Array.copyWithin
on MDN.
Examples
RESCRIPTlet arr = [100, 101, 102, 103, 104]
arr->Array.copyWithinToEnd(~target=0, ~start=2) == [102, 103, 104, 103, 104]
arr == [102, 103, 104, 103, 104]
copyWithin
let copyWithin: (
array<'a>,
~target: int,
~start: int,
~end: int=?,
) => array<'a>
copyWithin(array, ~target, ~start, ~end)
copies starting at element start
in the given array up to but not including end
to the designated target
position, returning the resulting array.
Beware this will mutate the array.
See Array.copyWithin
on MDN.
Examples
RESCRIPTlet arr = [100, 101, 102, 103, 104, 105]
arr->Array.copyWithin(~target=1, ~start=2, ~end=5) == [100, 102, 103, 104, 104, 105]
arr == [100, 102, 103, 104, 104, 105]
fillAll
Deprecated
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)
myArray == [9, 9, 9, 9]
fillToEnd
Deprecated
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)
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)
myArray == [9, 9, 9, 9]
myArray->Array.fill(0, ~start=1)
myArray == [9, 0, 0, 0]
myArray->Array.fill(5, ~start=1, ~end=3)
myArray == [9, 5, 5, 0]
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"]
someArray->Array.pop == Some("hello")
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")
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"])
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
someArray == ["hello", "hi"]
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"]
someArray->Array.shift == Some("hi")
someArray == ["hello"] // Notice first item is gone.
toSorted
let toSorted: (array<'a>, ('a, 'a) => 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]
someArray->Array.toSorted(Int.compare) == [1, 2, 3]
someArray == [3, 2, 1] // Original unchanged
sort
let sort: (array<'a>, ('a, 'a) => 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 array = [3, 2, 1]
array->Array.sort((a, b) => float(a - b))
array == [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>
removeInPlace
let removeInPlace: (array<'a>, int) => unit
removeInPlace(array, index)
removes the item at the specified index
from array
.
Beware this will mutate the array.
Examples
RESCRIPTlet array = []
array->Array.removeInPlace(0)
array == [] // Removing from an empty array does nothing
let array2 = ["Hello", "Hi", "Good bye"]
array2->Array.removeInPlace(1)
array2 == ["Hello", "Good bye"] // Removes the item at index 1
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")
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"])
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)
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
RESCRIPT[[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
RESCRIPT[1, 2]->Array.includes(1) == true
[1, 2]->Array.includes(3) == false
[{"language": "ReScript"}]->Array.includes({"language": "ReScript"}) == false // false, because of strict equality
indexOf
let indexOf: (array<'a>, 'a, ~from: int=?) => int
indexOf(array, item, ~from)
returns the index of the provided item
in array
, starting the search at from
. Uses strict check for equality when comparing items.
Returns -1
if the item isn't found. 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
RESCRIPT[1, 2]->Array.indexOf(2) == 1
[1, 2]->Array.indexOf(3) == -1
[1, 2, 1, 2]->Array.indexOf(2, ~from=2) == 3
[{"language": "ReScript"}]->Array.indexOf({"language": "ReScript"}) == -1 // -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
RESCRIPT[1, 2]->Array.indexOfOpt(2) == Some(1)
[1, 2]->Array.indexOfOpt(3) == None
[{"language": "ReScript"}]->Array.indexOfOpt({"language": "ReScript"}) == None // None, because of strict equality
indexOfFrom
Deprecated
let indexOfFrom: (array<'a>, 'a, int) => int
join
let join: (array<string>, string) => string
join(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 joinUnsafe
. Under the hood this will run JavaScript's toString
on all the array items.
See Array.join
Examples
RESCRIPT["One", "Two", "Three"]->Array.join(" -- ") == "One -- Two -- Three"
joinWith
Deprecated
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
RESCRIPT["One", "Two", "Three"]->Array.joinWith(" -- ") == "One -- Two -- Three"
joinUnsafe
let joinUnsafe: (array<'a>, string) => string
joinUnsafe(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.
See Array.join
Examples
RESCRIPT[1, 2, 3]->Array.joinUnsafe(" -- ") == "1 -- 2 -- 3"
joinWithUnsafe
Deprecated
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
RESCRIPT[1, 2, 3]->Array.joinWithUnsafe(" -- ") == "1 -- 2 -- 3"
lastIndexOf
let lastIndexOf: (array<'a>, 'a, ~from: int=?) => int
lastIndexOf(array, item, ~from)
returns the last index of the provided item
in array
, searching backwards from from
. Uses strict check for equality when comparing items.
Returns -1
if the item isn't found. Check out Array.lastIndexOfOpt
for a version that returns None
instead of -1
if the item does not exist.
See Array.lastIndexOf
on MDN.
Examples
RESCRIPT[1, 2, 1, 2]->Array.lastIndexOf(2) == 3
[1, 2]->Array.lastIndexOf(3) == -1
[1, 2, 1, 2]->Array.lastIndexOf(2, ~from=2) == 1
[{"language": "ReScript"}]->Array.lastIndexOf({"language": "ReScript"}) == -1 // -1, because of strict equality
lastIndexOfOpt
let lastIndexOfOpt: (array<'a>, 'a) => option<int>
lastIndexOfFrom
Deprecated
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
RESCRIPT[1, 2, 3, 4]->Array.slice(~start=1, ~end=3) == [2, 3]
[1, 2, 3, 4]->Array.slice(~start=1) == [2, 3, 4]
[1, 2, 3, 4]->Array.slice == [1, 2, 3, 4]
sliceToEnd
Deprecated
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
RESCRIPT[1, 2, 3, 4]->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
copyOfMyArray == [1, 2, 3]
(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
RESCRIPT[1, 2, 3, 4]->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]
array->Array.every(num => num <= 4) == true
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]
array->Array.everyWithIndex((num, index) => index < 5 && num <= 4) == true
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
RESCRIPT[1, 2, 3, 4]->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
RESCRIPT[1, 2, 3, 4]->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]
array->Array.find(item => item == ReScript) == Some(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]
array->Array.findWithIndex((item, index) => index > 1 && item == ReScript) == Some(ReScript)
findLast
let findLast: (array<'a>, 'a => bool) => option<'a>
findLast(array, checker)
returns the last element of array
where the provided checker
function returns true.
See Array.findLast
on MDN.
Examples
RESCRIPTlet array = [1, 2, 3]
array->Array.findLast(item => item > 0) == Some(3)
findLastWithIndex
let findLastWithIndex: (array<'a>, ('a, int) => bool) => option<'a>
findLastWithIndex(array, checker)
returns the last element of array
where the provided checker
function returns true.
See Array.findLast
on MDN.
Examples
RESCRIPTlet array = [1, 2, 3]
array->Array.findLastWithIndex((item, index) => index < 2 && item > 0) == Some(2)
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]
array->Array.findIndex(item => item == ReScript) == 0
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]
let isReScriptFirst =
array->Array.findIndexWithIndex((item, index) => index === 0 && item == ReScript)
let isTypeScriptFirst =
array->Array.findIndexWithIndex((item, index) => index === 0 && item == TypeScript)
isReScriptFirst == 0
isTypeScriptFirst == -1
findLastIndex
let findLastIndex: (array<'a>, 'a => bool) => int
findLastIndex(array, checker)
returns the index of the last element of array
where the provided checker
function returns true.
Returns -1
if the item does not exist. Consider using Array.findLastIndexOpt
if you want an option instead (where -1
would be None
).
See Array.findLastIndex
on MDN.
Examples
RESCRIPTtype languages = ReScript | TypeScript | JavaScript
let array = [ReScript, JavaScript, ReScript]
array->Array.findLastIndex(item => item == ReScript) == 2
array->Array.findLastIndex(item => item == TypeScript) == -1
findLastIndexWithIndex
let findLastIndexWithIndex: (array<'a>, ('a, int) => bool) => int
findLastIndexWithIndex(array, checker)
returns the index of the last element of array
where the provided checker
function returns true.
Returns -1
if the item does not exist. Consider using Array.findLastIndexOpt
if you want an option instead (where -1
would be None
).
See Array.findLastIndex
on MDN.
Examples
RESCRIPTtype languages = ReScript | TypeScript | JavaScript
let array = [ReScript, JavaScript, JavaScript, ReScript]
let isReScriptLast =
array->Array.findLastIndexWithIndex((item, index) => index === 3 && item == ReScript)
let isTypeScriptLast =
array->Array.findLastIndexWithIndex((item, index) => index === 3 && item == TypeScript)
isReScriptLast == 3
isTypeScriptLast == -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")
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))
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, init, fn)
Applies fn
to each element of xs
from beginning to end. Function fn
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.
Examples
RESCRIPTArray.reduce([2, 3, 4], 1, (a, b) => a + b) == 10
Array.reduce(["a", "b", "c", "d"], "", (a, b) => a ++ b) == "abcd"
[1, 2, 3]->Array.reduce(list{}, List.add) == list{3, 2, 1}
Array.reduce([], list{}, List.add) == list{}
reduceWithIndex
let reduceWithIndex: (array<'a>, 'b, ('b, 'a, int) => 'b) => 'b
reduceWithIndex(x, init, fn)
Applies fn
to each element of xs
from beginning to end. Function fn
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.
Examples
RESCRIPTArray.reduceWithIndex([1, 2, 3, 4], 0, (acc, x, i) => acc + x + i) == 16
Array.reduceWithIndex([1, 2, 3], list{}, (acc, v, i) => list{v + i, ...acc}) == list{5, 3, 1}
Array.reduceWithIndex([], list{}, (acc, v, i) => list{v + i, ...acc}) == list{}
reduceRight
let reduceRight: (array<'a>, 'b, ('b, 'a) => 'b) => 'b
reduceRight(xs, init, fn)
Works like Array.reduce
; except that function fn
is applied to each item of xs
from the last back to the first.
Examples
RESCRIPTArray.reduceRight(["a", "b", "c", "d"], "", (a, b) => a ++ b) == "dcba"
Array.reduceRight([1, 2, 3], list{}, List.add) == list{1, 2, 3}
Array.reduceRight([], list{}, List.add) == list{}
reduceRightWithIndex
let reduceRightWithIndex: (array<'a>, 'b, ('b, 'a, int) => 'b) => 'b
reduceRightWithIndex(xs, init, fn)
Like reduceRight
, but with an additional index argument on the callback function.
Examples
RESCRIPTArray.reduceRightWithIndex([1, 2, 3, 4], 0, (acc, x, i) => acc + x + i) == 16
Array.reduceRightWithIndex([], list{}, (acc, v, i) => list{v + i, ...acc}) == list{}
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"]
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"]
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")
array->Array.get(3) == None
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")
array[1] == Some("Hello")
getSymbol
let getSymbol: (array<'a>, Symbol.t) => option<'b>
getSymbolUnsafe
let getSymbolUnsafe: (array<'a>, Symbol.t) => 'b
setSymbol
let setSymbol: (array<'a>, 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 return undefined
value if index
does not exist in array
.
Use Array.getUnsafe
only when you are sure the index
exists (i.e. when using for-loop).
Examples
RESCRIPTlet array = [1, 2, 3]
for index in 0 to array->Array.length - 1 {
let value = array->Array.getUnsafe(index)
Console.log(value)
}
unsafe_get
Deprecated
let unsafe_get: (array<'a>, int) => 'a
unsafe_get(array, index)
returns the element at index
of array
.
This is unsafe, meaning it will return undefined
value if index
does not exist in array
.
Use Array.unsafe_get
only when you are sure the index
exists (i.e. when using for-loop).
Examples
RESCRIPTlet array = [1, 2, 3]
for index in 0 to array->Array.length - 1 {
let value = array->Array.unsafe_get(index)
Console.log(value)
}
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")
array[1] == Some("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]
array->Array.findIndexOpt(item => item == ReScript) == Some(0)
findLastIndexOpt
let findLastIndexOpt: (array<'a>, 'a => bool) => option<int>
findIndexOpt(array, checker)
returns the index of the last element of array
where the provided checker
function returns true.
Returns None
if no item matches.
See Array.findLastIndex
on MDN.
Examples
RESCRIPTlet array = ["hello", "world", "!"]
array->Array.findLastIndexOpt(item => item->String.includes("o")) == Some(1)
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
reversed == ["hello", "hi"]
someArray == ["hi", "hello"] // Original unchanged
filterMap
let filterMap: (array<'a>, 'a => option<'b>) => array<'b>
filterMap(array, fn)
Calls fn
for each element and returns a new array containing results of the fn
calls which are not None
.
Examples
RESCRIPT["Hello", "Hi", "Good bye"]->Array.filterMap(item =>
switch item {
| "Hello" => Some(item->String.length)
| _ => None
}
) == [5]
[1, 2, 3, 4, 5, 6]->Array.filterMap(n => mod(n, 2) == 0 ? Some(n * n) : None) == [4, 16, 36]
Array.filterMap([1, 2, 3, 4, 5, 6], _ => None) == []
Array.filterMap([], n => mod(n, 2) == 0 ? Some(n * n) : None) == []
filterMapWithIndex
let filterMapWithIndex: (array<'a>, ('a, int) => option<'b>) => array<'b>
filterMapWithIndex(array, fn)
Calls fn
for each element and returns a new array containing results of the fn
calls which are not None
.
Examples
RESCRIPT["Hello", "Hi", "Good bye"]->Array.filterMapWithIndex((item, index) =>
switch item {
| "Hello" => Some(index)
| _ => None
}
) == [0]
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
Examples
RESCRIPTArray.keepSome([Some(1), None, Some(3)]) == [1, 3]
Array.keepSome([Some(1), Some(2), Some(3)]) == [1, 2, 3]
Array.keepSome([None, None, None]) == []
Array.keepSome([]) == []
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)
Array.toShuffled([1, 2, 3])->Array.length == 3
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)
let array2 = [1, 2, 3]
array2->Array.shuffle
array2->Array.length == 3
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]
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]
flatMapWithIndex
let flatMapWithIndex: (array<'a>, ('a, int) => array<'b>) => array<'b>
flatMapWithIndex(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]
array->Array.flatMapWithIndex((item, index) =>
switch item {
| ReScript => [index]
| TypeScript => [index, index + 1]
| JavaScript => [index, index + 1, index + 2]
}
) == [0, 1, 2, 2, 3, 4]
findMap
let findMap: (array<'a>, 'a => option<'b>) => option<'b>
findMap(arr, fn)
Calls fn
for each element and returns the first value from fn
that is Some(_)
.
Otherwise returns None
Examples
RESCRIPTArray.findMap([1, 2, 3], n => mod(n, 2) == 0 ? Some(n - 2) : None) == Some(0)
Array.findMap([1, 2, 3, 4, 5, 6], n => mod(n, 2) == 0 ? Some(n - 8) : None) == Some(-6)
Array.findMap([1, 2, 3, 4, 5, 6], _ => None) == None
Array.findMap([], n => mod(n, 2) == 0 ? Some(n * n) : None) == None
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
last
let last: array<'a> => option<'a>
last(array)
returns the last element of array
.
Returns None
if the array is empty.
Examples
RESCRIPT["Hello", "Hi", "Good bye"]->Array.last == Some("Good bye")
[]->Array.last == None
ignore
let ignore: array<'a> => unit
ignore(array)
ignores the provided array and returns unit.
This helper is useful when you want to discard a value (for example, the result of an operation with side effects) without having to store or process it further.
entries
let entries: array<'a> => Iterator.t<(int, 'a)>
entries(array)
returns a new array iterator object that contains the key/value pairs for each index in the array.
See Array.prototype.entries on MDN.
Examples
RESCRIPTlet array = [5, 6, 7]
let iterator: Iterator.t<(int, int)> = array->Array.entries
iterator->Iterator.next == {done: false, value: Some((0, 5))}
iterator->Iterator.next == {done: false, value: Some((1, 6))}
values
let values: array<'a> => Iterator.t<'a>
values(array)
returns a new array iterator object that contains the values for each index in the array.
See Array.prototype.values on MDN.
Examples
RESCRIPTlet array = [5, 6, 7]
let iterator: Iterator.t<int> = array->Array.values
iterator->Iterator.next == {done: false, value: Some(5)}
iterator->Iterator.next == {done: false, value: Some(6)}