Array

append

Append an element to the end of an array, creating a new array.

Example

RESCRIPT
[2, 3]->Array.append(1) // => [2, 3, 1]

concat

The concat function is used to merge two or more arrays. This method does not change the existing arrays, but instead returns a new array.

Zero-cost binding to Javascript's Array.prototype.concat.

Example

RESCRIPT
[1, 2, 3]->Array.concat([4, 5, 6]) // => [1, 2, 3, 4, 5, 6]

copy

This function takes an array and makes a new array containing the same elements.

Zero-cost binding to Javascript's Array.prototype.slice.

Example

RESCRIPT
[1, 2, 3]->Array.copy // => [1, 2, 3]

deleteAt

Delete the element at the specified index, creating a new array, or returning None if the index is out of bounds.

Example

RESCRIPT
[1, 2, 3, 4]->Array.deleteAt(0) // => Some([2, 3, 4]) [1, 2, 3, 4]->Array.deleteAt(10) // => None

every

every tells if the provided predicate holds true for every element in the Array.

Zero-cost binding to Javascript's Array.prototype.every.

Example

RESCRIPT
[1, 1, 1]->Array.every(n => n == 1) // => true [1, 2, 3]->Array.every(n => n == 1) // => false

filter

Given an iterating function that is a Predicate or a Refinement, filter creates a new Array containing the elements of the original Array for which the iterating function is true.

Zero-cost binding to Javascript's Array.prototype.filter.

Example

RESCRIPT
[1, 2, 3]->Array.filter(n => n <= 2) // => [1, 2]

filterWithIndex

Similar to filter but it includes the item's index.

Zero-cost binding to Javascript's Array.prototype.filter.

Example

RESCRIPT
[1, 2, 3]->Array.filterWithIndex((_, i) => i < 1) // => [1]

findFirst

Find the first element which satisfies a predicate (or a refinement) function. It returns an Option containing the element or None if not found.

Example

RESCRIPT
[1, 2, 3]->Array.findFirst(n => n == 1) // => Some(1) [1, 2, 3]->Array.findFirst(n => n == 100) // => None

findIndex

findIndex returns an Option containing the first index for which a predicate holds. It returns None if no element satisfies the predicate. Similar to findFirst but returning the index instead of the element.

Example

RESCRIPT
[1, 2, 3]->Array.findIndex(n => n == 1) // => Some(0) [1, 2, 3]->Array.findIndex(n => n == 10) // => None

findUnsafeIndex

Returns the index of the first element in an array that satisfies the provided testing function. If no elements satisfy the testing function, -1 is returned.

Zero-cost binding to Javascript's Array.prototype.findIndex.

Example

RESCRIPT
[1, 2, 3]->Array.findUnsafeIndex(n => n == 1)->expect->toEqual(0) [1, 2, 3]->Array.findUnsafeIndex(n => n == 10)->expect->toEqual(-1)

flatten

Takes an array of arrays of 'a and flattens them into an array of 'a by concatenating the elements of each array in order.

Example

RESCRIPT
[[1, 2], [3, 4], [5, 6]]->Array.flatten // => [1, 2, 3, 4, 5, 6]

forEach

Executes a provided function once for each array element.

Example

RESCRIPT
[1, 2, 3]->Array.forEach(Console.log) // logs 1,2,3 to the console

get

Gets the value from the given index, or None if the index is out of bounds.

Example

RESCRIPT
[1, 2, 3]->Array.get(1) // => Some(2)

head

Get the first element in an array, or None if the array is empty

Example

RESCRIPT
[1, 2, 3]->Array.head // => Some(1) []->Array.head // => NoneA functional standard library for ReScript.

insertAt

Inserts an element at the specified index, creating a new array, or returning None if the index is out of bounds.

Example

RESCRIPT
[1, 2, 3, 4]->Array.insertAt(0, 100) // => Some([100, 1, 2, 3, 4]) [1, 2, 3, 4]->Array.insertAt(10, 100) // => None []->Array.insertAt(0, 100)->expect-> // => Some([100])

isEmpty

Test whether an array is empty

Example

RESCRIPT
[]->Array.isEmpty // => true [1, 2, 3]->Array.isEmpty // => false

last

Get the last element in an array, or None if the array is empty

Example

RESCRIPT
[1, 2, 3]->Array.last // => Some(3) []->Array.last // => None

length

Calculate the number of elements in a Array.

Zero-cost binding to Javascript's Array.prototype.length.

Example

RESCRIPT
[1, 2, 3]->Array.length // => 3

map

The map function creates a new array populated with the results of calling a provided function on every element in the calling array.

Zero-cost binding to Javascript's Array.prototype.map.

Example

RESCRIPT
[1, 2, 3]->Array.map(n => n + 1) // => [2,3,4]

mapWithIndex

Similar to map but it also includes the array index of each item.

Zero-cost binding to Javascript's Array.prototype.map.

Example

RESCRIPT
[1, 2, 3]->Array.mapWithIndex((n, i) => n + i) // => [1, 3, 5]

modifyAt

Apply a function to the element at the specified index, creating a new array, or returning None if the index is out of bounds.

Example

RESCRIPT
[1, 2, 3]->Array.modifyAt(0, n => n + 100) // => Some([101, 2, 3])) [1, 2, 3]->Array.modifyAt(100, n => n + 100) // => None

reduce

Reduces an Array.

reduce executes the supplied iterating function on each element of the array, in order, passing in the element and the return value from the calculation on the preceding element.

The first time that the iterating function is called there is no “return value of the previous calculation”, the initial value is used in its place.

Zero-cost binding to Javascript's Array.prototype.reduce.

Example

RESCRIPT
[1, 2, 3]->Array.reduce(0, (item, acc) => item + acc) // => 6

reduceWithIndex

Similar to reduce but it includes each item's index.

Zero-cost binding to Javascript's Array.prototype.reduce.

Example

RESCRIPT
[1, 2, 3]->Array.reduceWithIndex(0, (item, i, acc) => item + i + acc) // => 9

Same as reduce but applied from the end to the start.

Zero-cost binding to Javascript's Array.prototype.reduceRight.

Example

RESCRIPT
[1, 2, 3]->Array.reduceRight(0, (item, acc) => item + acc) // => 6

Same as reduceRight but includes the index for each item.

Zero-cost binding to Javascript's Array.prototype.reduceRight.

Example

RESCRIPT
[1, 2, 3]->Array.reduceRightWithIndex(0, (item, i, acc) => item + i + acc) // => 9

reverse

Reverse an array, creating a new array

Example

RESCRIPT
[1, 2, 3]->Array.reverse // => [3, 2, 1]

slice

Slices the array starting at the first int and ending just before the second int.

Returns Some<array<'a>> if start and end are in bounds. Returns None if start or end are out of bounds, or if start is greater than end.

Example

RESCRIPT
[1, 2, 3, 4]->Array.slice(1, 4) // => Some([2, 3, 4]) [1, 2, 3, 4]->Array.slice(10, 20) // => None

sliceUnsafe

Similar to slice but will return array<'a> instead of Some<array<'a>> and empty array instead of None

RESCRIPT
[1, 2, 3, 4]->Array.slice(1, 4) // => [2, 3, 4] [1, 2, 3, 4]->Array.slice(10, 20) // => []

some

some tells if the provided predicate holds true at least for one element in the Array.

Zero-cost binding to Javascript's Array.prototype.some.

Example

RESCRIPT
[1, 2, 3]->Array.some(n => n == 1) // => true [1, 2, 3]->Array.some(n => n == 10)// => false

someWithIndex

Similar to some but it includes an index with each item.

Zero-cost binding to Javascript's Array.prototype.some.

Example

RESCRIPT
[1, 2, 3]->Array.someWithIndex((n, i) => n + i == 1) // => true [1, 2, 3]->Array.someWithIndex((n, i) => n + i == 10) // => false

sort

The sort function returns a sorted copy of the array using a compare function.

The compare function takes in two elements of the array at a time, (a,b) and it will sort the items depending on the int returned.

Zero-cost binding to Javascript's Array.prototype.toSorted.

> 0 - sort a after b, e.g. [b, a]

< 0 - sort a before b, e.g. [a, b]

=== 0 - keep original order of a and b

Example

RESCRIPT
[3, 6, 2]->Array.sort((a, b) => a > b ? 0 : -1) // => [2, 3, 6]

splice

The splice() function returns a new array with some elements removed and/or replaced at a given index.

Zero-cost binding to Javascript's Array.prototype.toSpliced.

Example

RESCRIPT
[1, 2, 3]->Array.splice(~start=2, ~insert=[7, 8], ~remove=1) // => [1, 2, 7, 8]

tail

Get all but the first element of an array, creating a new array, or None if the array is empty

Example

RESCRIPT
[1, 2, 3]->Array.tail // => Some([2, 3]) [1]->Array.tail // =>None

prepend

Prepend an element to the start of an array, creating a new array.

Example

RESCRIPT
[2, 3]->Array.prepend(1) // => [1, 2, 3]

updateAt

Change the element at the specified index, creating a new array, or returning None if the index is out of bounds.

Example

RESCRIPT
[1, 2, 3]->Array.updateAt(0, 100) // =>Some([100, 2, 3]) [1, 2, 3]->Array.updateAt(100, 100) // => None