Array
Edit on GitHubUtilities for working with arrays.
Values
Array.length
Returns the length of the input array.
Array.make
Array.make(n, value)
creates a new array of length n
filled with value
.
Array.init
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.
Array.get
An alias for normal syntactic array access, i.e. array[n]
.
Array.get(n, array)
retrieves the n
th item from the array. A negative n
is treated as an offset from the end of the array.
Array.set
An alias for normal syntactic array set, i.e. array[n] := value
.
Array.set(n, value, array)
sets the n
th 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
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
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
Produces a copy of the input array.
Array.forEach
Array.forEach(fn, array)
calls fn
on each element of the array.
Array.forEachi
Array.forEachi(fn, array)
calls fn
on each element of the array, along with the index of the element.
Array.map
Array.map(fn, array)
produces a new array by calling fn
on each element of the input array.
Array.mapi
Array.mapi(fn, array)
produces a new array by calling fn
on each element of the input array along with its index.
Array.reduce
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
.
Array.reducei
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
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
Checks that the given condition is satisfied for all items in the input array.
Array.some
Checks that the given condition is satisfied at least once by an item in the input array.
Array.fill
Replaces all elements in an array with the new value provided.
Array.fillRange
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
Converts the input array to a list.
Array.fromList
Converts the input list to an array.
Array.contains
Checks if the item is an element of the input array. Uses the generic ==
structural equality operator.
Array.find
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
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
Combines two arrays into a Cartesian product of tuples containing all ordered pairs (a, b)
.
Array.count
Counts the number of elements in a array that satisfy the given condition.
Array.counti
Counts the number of elements in a array that satisfy the given condition. The condition function received the element and its index.
Array.filter
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
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
Produces a new array with any duplicates removed. Uses the generic ==
structural equality operator.