Array

GitHub   Edit on GitHub

Utilities for working with arrays.

1
import Array from "array"

Values

Array.length

1
length : Array<a> -> Number

Returns the length of the input array.

Array.make

1
make : (Number, a) -> Array<a>

Array.make(n, value) creates a new array of length n filled with value.

1
Array.make(5, "foo") // [> "foo", "foo", "foo", "foo", "foo"]

Array.init

1
init : (Number, Number -> a) -> Array<a>

Array.init(n, fn) creates a new array of length n where each value is initialized with the result of the initializer fn. The initializer is called with the index of each array element.

1
Array.init(5, n => n + 3) // [> 8, 9, 10, 11, 12]

Array.get

1
get : (Number, Array<a>) -> a

An alias for normal syntactic array access, i.e. array[n].

Array.get(n, array) retrieves the nth item from the array. A negative n is treated as an offset from the end of the array.

Array.set

1
set : (Number, a, Array<a>) -> a

An alias for normal syntactic array set, i.e. array[n] := value.

Array.set(n, value, array) sets the nth slot in the array to value. A negative n is treated as an offset from the end of the array. Returns the newly set item.

Array.append

1
append : (Array<a>, Array<a>) -> Array<a>

Array.append(array1, array2) creates a new array with the items of array1 followed by the items of array2. This does not modify array1 or array2.

Array.concat

1
concat : List<Array<a>> -> Array<a>

Takes a list of arrays and creates a single array containing the elements of all of the arrays. Does not modify any of the input arrays.

Array.copy

1
copy : Array<a> -> Array<a>

Produces a copy of the input array.

Array.forEach

1
forEach : (a -> Void, Array<a>) -> Void

Array.forEach(fn, array) calls fn on each element of the array.

Array.forEachi

1
forEachi : ((a, Number) -> Void, Array<a>) -> Void

Array.forEachi(fn, array) calls fn on each element of the array, along with the index of the element.

Array.map

1
map : (a -> b, Array<a>) -> Array<b>

Array.map(fn, array) produces a new array by calling fn on each element of the input array.

Array.mapi

1
mapi : ((a, Number) -> b, Array<a>) -> Array<b>

Array.mapi(fn, array) produces a new array by calling fn on each element of the input array along with its index.

Array.reduce

1
reduce : ((b, a) -> b, b, Array<a>) -> b

Using a reducer function, reduce combines all elements of an array, starting from the “head”, or left side, of the array.

In Array.reduce(fn, base, array), fn is called with the accumulator and each element of the array, and returns a new accumulator. The final value is the result of reduce.
The accumulator starts with value base.

1
2
3
let add = a + b
let sum = (array) => Array.reduce(add, 0, array)
sum([1, 2, 3]) // 6

Array.reducei

1
reducei : ((b, a, Number) -> b, b, Array<a>) -> b

Using a reducer function, reducei combines all elements of an array, starting from the “head”, or left side, of the array.

In Array.reducei(fn, base, array), fn is called with the accumulator, each element of the array, and the index of that element. It returns a new accumulator. The final value is the result of reducei.
The accumulator starts with value base.

Array.flatMap

1
flatMap : ((a) -> Array<b>, Array<a>) -> Array<b>

Array.flatMap(fn, array) produces a new array by calling fn on each element of the input array. Each iteration produces an intermediate array, which are all appended to produce a “flattened” array of all results.

Array.every

1
every : (a -> Bool, Array<a>) -> Bool

Checks that the given condition is satisfied for all items in the input array.

Array.some

1
some : (a -> Bool, Array<a>) -> Bool

Checks that the given condition is satisfied at least once by an item in the input array.

Array.fill

1
fill : (a, Array<a>) -> Void

Replaces all elements in an array with the new value provided.

Array.fillRange

1
fillRange : (a, Number, Number, Array<a>) -> Void

Array.fillRange(value, start, stop, array) replaces all elements between start and stop in the array with the new value provided. Fails if the index is out-of-bounds.

Array.toList

1
toList : Array<a> -> List<a>

Converts the input array to a list.

Array.fromList

1
fromList : List<a> -> Array<a>

Converts the input list to an array.

Array.contains

1
contains : (a, Array<a>) -> Bool

Checks if the item is an element of the input array. Uses the generic == structural equality operator.

Array.find

1
find : (a -> Bool, Array<a>) -> Option<a>

Array.find(fn, array) calls fn on each element of the array and returns Some(element) containing the first element for which fn returns true or None if no element was found.

Array.findIndex

1
findIndex : (a -> Bool, Array<a>) -> Option<Number>

Array.findIndex(fn, array) calls fn on each element of the array and returns Some(index) containing the index of the first element for which fn returns true or None if no element was found.

Array.product

1
product : (Array<a>, Array<b>) -> Array<(a, b)>

Combines two arrays into a Cartesian product of tuples containing all ordered pairs (a, b).

Array.count

1
count : (a -> Bool, Array<a>) -> Number

Counts the number of elements in a array that satisfy the given condition.

Array.counti

1
counti : ((a, Number) -> Bool, Array<a>) -> Number

Counts the number of elements in a array that satisfy the given condition. The condition function received the element and its index.

Array.filter

1
filter : (a -> Bool, Array<a>) -> Array<a>

Array.filter(fn, array) produces a new array by calling fn on each element of the array. If the fn returns false, the value will not be included the new array.

Array.filteri

1
filteri : ((a, Number) -> Bool, Array<a>) -> Array<a>

Array.filter(fn, array) produces a new array by calling fn on each element of the array and its index. If the fn returns false, the value will not be included the new array.

Array.unique

1
unique : Array<a> -> Array<a>

Produces a new array with any duplicates removed. Uses the generic == structural equality operator.

This is a notification!