API / Core / Array

Array

fromIterator

RESCRIPT
let fromIterator: Core__Iterator.t<'a> => array<'a>

fromIterator(iterator)

Creates an array from the provided iterator

RES
let map = Map.fromArray([("foo", 1), ("bar", 2)]) Array.fromIterator(map->Map.values) // [1, 2]

fromArrayLike

RESCRIPT
let fromArrayLike: Js.Array2.array_like<'a> => array<'a>

fromArrayLikeWithMap

RESCRIPT
let fromArrayLikeWithMap: (Js.Array2.array_like<'a>, 'a => 'b) => array<'b>

make

RESCRIPT
let make: (~length: int, 'a) => array<'a>

make(~length, init)

Creates an array of length length initialized with the value of init.

RES
Array.make(~length=3, #apple) == [#apple, #apple, #apple]

fromInitializer

RESCRIPT
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.

RES
Array.fromInitializer(~length=3, i => i + 3) == [3, 4, 5]

equal

RESCRIPT
let equal: (array<'a>, array<'a>, ('a, 'a) => bool) => bool

compare

RESCRIPT
let compare: ( array<'a>, array<'a>, ('a, 'a) => Core__Ordering.t, ) => Core__Ordering.t

isArray

RESCRIPT
let isArray: 'a => bool

length

RESCRIPT
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

RESCRIPT
let someArray = ["hi", "hello"] Console.log(someArray->Array.length) // 2

copyAllWithin

RESCRIPT
let copyAllWithin: (array<'a>, ~target: int) => array<'a>

copyWithinToEnd

RESCRIPT
let copyWithinToEnd: (array<'a>, ~target: int, ~start: int) => array<'a>

copyWithin

RESCRIPT
let copyWithin: ( array<'a>, ~target: int, ~start: int, ~end: int, ) => array<'a>

fillAll

RESCRIPT
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

RESCRIPT
let myArray = [1, 2, 3, 4] myArray->Array.fillAll(9) Console.log(myArray) // [9, 9, 9, 9]

fillToEnd

RESCRIPT
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

RESCRIPT
let myArray = [1, 2, 3, 4] myArray->Array.fillToEnd(9, ~start=1) Console.log(myArray) // [1, 9, 9, 9]

fill

RESCRIPT
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

RESCRIPT
let myArray = [1, 2, 3, 4] myArray->Array.fill(9, ~start=1, ~end=2) Console.log(myArray) // [1, 9, 9, 4]

pop

RESCRIPT
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

RESCRIPT
let someArray = ["hi", "hello"] let lastItem = someArray->Array.pop // "hello" Console.log(someArray) // ["hi"]. Notice last item is gone.

push

RESCRIPT
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

RESCRIPT
let someArray = ["hi", "hello"] someArray->Array.push("yay") Console.log(someArray) // ["hi", "hello", "yay"]

pushMany

RESCRIPT
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

RESCRIPT
let someArray = ["hi", "hello"] someArray->Array.pushMany(["yay", "wehoo"]) Console.log(someArray) // ["hi", "hello", "yay", "wehoo"]

reverse

RESCRIPT
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

RESCRIPT
let someArray = ["hi", "hello"] someArray->Array.reverse Console.log(someArray) // ["hello", "h1"]

shift

RESCRIPT
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

RESCRIPT
let someArray = ["hi", "hello"] let lastItem = someArray->Array.shift // "hi" Console.log(someArray) // ["hello"]. Notice first item is gone.

toSorted

RESCRIPT
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

RESCRIPT
let 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

RESCRIPT
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

RESCRIPT
let someArray = [3, 2, 1] someArray->Array.sort((a, b) => float(a - b)) Console.log(someArray) // [1, 2, 3]

splice

RESCRIPT
let splice: ( array<'a>, ~start: int, ~remove: int, ~insert: array<'a>, ) => unit

toSpliced

RESCRIPT
let toSpliced: ( array<'a>, ~start: int, ~remove: int, ~insert: array<'a>, ) => array<'a>

with

RESCRIPT
let with: (array<'a>, int, 'a) => array<'a>

unshift

RESCRIPT
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

RESCRIPT
let someArray = ["hi", "hello"] someArray->Array.unshift("yay") Console.log(someArray) // ["yay", "hi", "hello"]

unshiftMany

RESCRIPT
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

RESCRIPT
let someArray = ["hi", "hello"] someArray->Array.unshiftMany(["yay", "wehoo"]) Console.log(someArray) // ["yay", "wehoo", "hi", "hello"]

concat

RESCRIPT
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

RESCRIPT
let array1 = ["hi", "hello"] let array2 = ["yay", "wehoo"] let someArray = array1->Array.concat(array2) Console.log(someArray) // ["hi", "hello", "yay", "wehoo"]

concatMany

RESCRIPT
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

RESCRIPT
let array1 = ["hi", "hello"] let array2 = ["yay"] let array3 = ["wehoo"] let someArray = array1->Array.concatMany([array2, array3]) Console.log(someArray) // ["hi", "hello", "yay", "wehoo"]

flat

RESCRIPT
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
Console.log([[1], [2], [3, 4]]->Array.flat) // [1, 2, 3, 4]

includes

RESCRIPT
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
Console.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

RESCRIPT
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

RESCRIPT
Console.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

RESCRIPT
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
Console.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

RESCRIPT
let indexOfFrom: (array<'a>, 'a, int) => int

join

RESCRIPT
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
let array = ["One", "Two", "Three"] Console.log(array->Array.join(" -- ")) // One -- Two -- Three

joinWith

Deprecated

Use join instead

RESCRIPT
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
let array = ["One", "Two", "Three"] Console.log(array->Array.joinWith(" -- ")) // One -- Two -- Three

joinUnsafe

RESCRIPT
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
let array = [1, 2, 3] Console.log(array->Array.joinUnsafe(" -- ")) // 1 -- 2 -- 3

joinWithUnsafe

Deprecated

Use joinUnsafe instead

RESCRIPT
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
let array = [1, 2, 3] Console.log(array->Array.joinWithUnsafe(" -- ")) // 1 -- 2 -- 3

lastIndexOf

RESCRIPT
let lastIndexOf: (array<'a>, 'a) => int

lastIndexOfOpt

RESCRIPT
let lastIndexOfOpt: (array<'a>, 'a) => option<int>

lastIndexOfFrom

RESCRIPT
let lastIndexOfFrom: (array<'a>, 'a, int) => int

slice

RESCRIPT
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
let myArray = [1, 2, 3, 4] Console.log(myArray->Array.slice(~start=1, ~end=3)) // [2, 3]

sliceToEnd

RESCRIPT
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
let myArray = [1, 2, 3, 4] Console.log(myArray->Array.sliceToEnd(~start=1)) // [2, 3, 4]

copy

RESCRIPT
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

RESCRIPT
let myArray = [1, 2, 3] let copyOfMyArray = myArray->Array.copy Console.log(copyOfMyArray) // [1, 2, 3] Console.log(myArray === copyOfMyArray) // false

toString

RESCRIPT
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
let array = [1, 2, 3, 4] Console.log(array->Array.toString) // "1,2,3,4"

toLocaleString

RESCRIPT
let toLocaleString: array<'a> => string

every

RESCRIPT
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

RESCRIPT
let array = [1, 2, 3, 4] Console.log(array->Array.every(num => num > 4)) // true Console.log(array->Array.every(num => num === 1)) // false

everyWithIndex

RESCRIPT
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

RESCRIPT
let 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

RESCRIPT
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
let array = [1, 2, 3, 4] Console.log(array->Array.filter(num => num > 2)) // [3, 4]

filterWithIndex

RESCRIPT
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
let array = [1, 2, 3, 4] Console.log(array->Array.filterWithIndex((num, index) => index === 0 || num === 2)) // [1, 2]

find

RESCRIPT
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

RESCRIPT
type 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

RESCRIPT
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

RESCRIPT
type 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

RESCRIPT
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

RESCRIPT
type 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

RESCRIPT
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

RESCRIPT
type 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) Console.log(isReScriptFirst) // 0 Console.log(isTypeScriptFirst) // -1

forEach

RESCRIPT
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

RESCRIPT
let array = ["Hello", "Hi", "Good bye"] array->Array.forEach(item => { Console.log(item) })

forEachWithIndex

RESCRIPT
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

RESCRIPT
let array = ["Hello", "Hi", "Good bye"] array->Array.forEachWithIndex((item, index) => { Console.log("At item " ++ Int.toString(index) ++ ": " ++ item) })

map

RESCRIPT
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

RESCRIPT
let 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

RESCRIPT
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

RESCRIPT
let 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

RESCRIPT
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.

RES
Array.reduce([2, 3, 4], 1, (a, b) => a + b) == 10 Array.reduce(["a", "b", "c", "d"], "", (a, b) => a ++ b) == "abcd"

reduceWithIndex

RESCRIPT
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.

RES
Array.reduceWithIndex([1, 2, 3, 4], 0, (acc, x, i) => acc + x + i) == 16

reduceRight

RESCRIPT
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.

RES
Array.reduceRight(["a", "b", "c", "d"], "", (a, b) => a ++ b) == "dcba"

reduceRightWithIndex

RESCRIPT
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.

RES
Array.reduceRightWithIndex([1, 2, 3, 4], 0, (acc, x, i) => acc + x + i) == 16

some

RESCRIPT
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

RESCRIPT
let array = ["Hello", "Hi", "Good bye"] Console.log(array->Array.some(greeting => greeting === "Hello")) // true

someWithIndex

RESCRIPT
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

RESCRIPT
let array = ["Hello", "Hi", "Good bye"] Console.log(array->Array.someWithIndex((greeting, index) => greeting === "Hello" && index === 0)) // true

get

RESCRIPT
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

RESCRIPT
let array = ["Hello", "Hi", "Good bye"] array->Array.get(0) == Some("Hello") // true array->Array.get(3) == None // true

set

RESCRIPT
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

RESCRIPT
let array = ["Hello", "Hi", "Good bye"] array->Array.set(1, "Hello") Console.log(array[1]) // "Hello"

getSymbol

RESCRIPT
let getSymbol: (array<'a>, Core__Symbol.t) => option<'b>

getSymbolUnsafe

RESCRIPT
let getSymbolUnsafe: (array<'a>, Core__Symbol.t) => 'b

setSymbol

RESCRIPT
let setSymbol: (array<'a>, Core__Symbol.t, 'b) => unit

getUnsafe

RESCRIPT
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

RESCRIPT
let array = [1, 2, 3] for index in 0 to array->Array.length - 1 { let value = array->Array.getUnsafe(index) Console.log(value) }

setUnsafe

RESCRIPT
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

RESCRIPT
let array = ["Hello", "Hi", "Good bye"] array->Array.setUnsafe(1, "Hello") Console.log(array[1]) // "Hello"

findIndexOpt

RESCRIPT
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

RESCRIPT
type 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

RESCRIPT
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

RESCRIPT
let someArray = ["hi", "hello"] let reversed = someArray->Array.toReversed Console.log(reversed) // ["hello", "h1"] Console.log(someArray) // ["h1", "hello"]. Original unchanged

filterMap

RESCRIPT
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
let array = ["Hello", "Hi", "Good bye"] Console.log( array->Array.filterMap(item => switch item { | "Hello" => Some(item->String.length) | _ => None } ), ) // [5]

keepSome

RESCRIPT
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

RES
Array.keepSome([Some(1), None, Some(3)]) == [1, 3]

toShuffled

RESCRIPT
let toShuffled: array<'a> => array<'a>

toShuffled(array) returns a new array with all items in array in a random order.

Examples

RESCRIPT
let array = ["Hello", "Hi", "Good bye"] let shuffledArray = array->Array.toShuffled Console.log(shuffledArray)

shuffle

RESCRIPT
let shuffle: array<'a> => unit

shuffle(array) randomizes the position of all items in array.

Beware this will mutate the array.

Examples

RESCRIPT
let array = ["Hello", "Hi", "Good bye"] array->Array.shuffle Console.log(array)

flatMap

RESCRIPT
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

RESCRIPT
type 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]

flatMapWithIndex

RESCRIPT
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

RESCRIPT
type language = ReScript | TypeScript | JavaScript let array = [ReScript, TypeScript, JavaScript] Console.log( 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

RESCRIPT
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

RES
Array.findMap([1, 2, 3], n => mod(n, 2) == 0 ? Some(n - 2) : None) == Some(0) // true

at

RESCRIPT
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

RESCRIPT
let last: array<'a> => option<'a>

last(array) returns the last element of array.

Returns None if the array is empty.

Examples

RESCRIPT
let array = ["Hello", "Hi", "Good bye"] array->Array.last == Some("Good bye") // true []->Array.last == None // true