Array
Edit on GitHubUtilities for working with arrays.
An immutable array implementation is available in the Immutable
submodule.
Added in 0.2.0
version | changes |
---|---|
0.1.0 | Originally named `arrays` |
0.2.0 | Renamed to `array` |
Values
Functions and constants included in the Array module.
Array.length
Added in 0.1.0
No other changes yet.
Provides the length of the input array.
Parameters:
param | type | description |
---|---|---|
array |
Array<a> |
The array to inspect |
Returns:
type | description |
---|---|
Number |
The number of elements in the array |
Examples:
Array.make
Added in 0.1.0
No other changes yet.
Creates a new array of the specified length with each element being initialized with the given value.
Parameters:
param | type | description |
---|---|---|
length |
Number |
The length of the new array |
item |
a |
The value to store at each index |
Returns:
type | description |
---|---|
Array<a> |
The new array |
Throws:
InvalidArgument(String)
- When
length
is not an integer - When
length
is negative
Examples:
Array.init
Added in 0.1.0
No other changes yet.
Creates a new array of the specified length where each element is initialized with the result of an initializer function. The initializer is called with the index of each array element.
Parameters:
param | type | description |
---|---|---|
length |
Number |
The length of the new array |
fn |
Number => a |
The initializer function to call with each index, where the value returned will be used to initialize the element |
Returns:
type | description |
---|---|
Array<a> |
The new array |
Throws:
InvalidArgument(String)
- When
length
is not an integer - When
length
is negative
Examples:
Array.get
Added in 0.1.0
version | changes |
---|---|
0.2.0 | Argument order changed to data-last |
An alias for normal syntactic array access, i.e. array[n]
.
Retrieves the element from the array at the specified index. A negative index is treated as an offset from the end of the array.
Parameters:
param | type | description |
---|---|---|
index |
Number |
The index to access |
array |
Array<a> |
The array to access |
Returns:
type | description |
---|---|
a |
The element from the array |
Throws:
IndexOutOfBounds
- When
index
is not an integer - When
index
is out of bounds
Examples:
Array.set
Added in 0.1.0
version | changes |
---|---|
0.2.0 | Argument order changed to data-last |
An alias for normal syntactic array set, i.e. array[n] = value
.
Sets the element at the specified index in the array to the new value. A negative index is treated as an offset from the end of the array.
Parameters:
param | type | description |
---|---|---|
index |
Number |
The index to update |
value |
a |
The value to store |
array |
Array<a> |
The array to update |
Throws:
IndexOutOfBounds
- When
index
is not an integer - When
index
is out of bounds
Examples:
Array.append
Added in 0.1.0
No other changes yet.
Creates a new array with the elements of the first array followed by the elements of the second array. This does not modify the arguments.
Parameters:
param | type | description |
---|---|---|
array1 |
Array<a> |
The array containing elements to appear first |
array2 |
Array<a> |
The array containing elements to appear second |
Returns:
type | description |
---|---|
Array<a> |
The new array containing elements from array1 followed by elements from array2 |
Throws:
InvalidArgument(String)
- When the combined length of the two arrays is not an integer
Examples:
Array.concat
Added in 0.1.0
No other changes yet.
Creates a single array containing the elements of all arrays in the provided list. Does not modify any of the input arguments.
Parameters:
param | type | description |
---|---|---|
arrays |
List<Array<a>> |
A list containing all arrays to combine |
Returns:
type | description |
---|---|
Array<a> |
The new array |
Throws:
InvalidArgument(String)
- When the combined length of all arrays is not an integer
Examples:
Array.copy
Added in 0.1.0
No other changes yet.
Produces a shallow copy of the input array. The new array contains the same elements as the original.
Parameters:
param | type | description |
---|---|---|
array |
Array<a> |
The array to copy |
Returns:
type | description |
---|---|
Array<a> |
The new array containing the elements from the input |
Examples:
Array.cycle
Added in 0.4.4
No other changes yet.
Iterates an array a given number of times, calling an iterator function on each element.
Parameters:
param | type | description |
---|---|---|
fn |
a => Void |
The iterator function to call with each element |
n |
Number |
The number of times to iterate the given array |
array |
Array<a> |
The array to iterate |
Examples:
Array.forEach
Added in 0.1.0
version | changes |
---|---|
0.2.0 | Argument order changed to data-last |
Iterates an array, calling an iterator function on each element.
Parameters:
param | type | description |
---|---|---|
fn |
a => Void |
The iterator function to call with each element |
array |
Array<a> |
The array to iterate |
Examples:
Array.forEachi
Added in 0.1.0
version | changes |
---|---|
0.2.0 | Argument order changed to data-last |
Iterates an array, calling an iterator function on each element. Also passes the index as the second argument to the function.
Parameters:
param | type | description |
---|---|---|
fn |
(a, Number) => Void |
The iterator function to call with each element |
array |
Array<a> |
The array to iterate |
Examples:
1 | let mut str = "" |
Array.map
Added in 0.1.0
version | changes |
---|---|
0.2.0 | Argument order changed to data-last |
Produces a new array initialized with the results of a mapper function called on each element of the input array.
Parameters:
param | type | description |
---|---|---|
fn |
a => b |
The mapper function to call on each element, where the value returned will be used to initialize the element in the new array |
array |
Array<a> |
The array to iterate |
Returns:
type | description |
---|---|
Array<b> |
The new array with mapped values |
Examples:
Array.mapi
Added in 0.1.0
No other changes yet.
Produces a new array initialized with the results of a mapper function called on each element of the input array and its index.
Parameters:
param | type | description |
---|---|---|
fn |
(a, Number) => b |
The mapper function to call on each element, where the value returned will be used to initialize the element in the new array |
array |
Array<a> |
The array to iterate |
Returns:
type | description |
---|---|
Array<b> |
The new array with mapped values |
Examples:
Array.reduce
Added in 0.3.0
No other changes yet.
Combines all elements of an array using a reducer function, starting from the “head”, or left side, of the array.
In Array.reduce(fn, initial, array)
, fn
is called with
an accumulator and each element of the array, and returns
a new accumulator. The final value is the last accumulator
returned. The accumulator starts with value initial
.
Parameters:
param | type | description |
---|---|---|
fn |
(a, b) => a |
The reducer function to call on each element, where the value returned will be the next accumulator value |
initial |
a |
The initial value to use for the accumulator on the first iteration |
array |
Array<b> |
The array to iterate |
Returns:
type | description |
---|---|
a |
The final accumulator returned from fn |
Examples:
Array.reduceRight
Added in 0.5.3
No other changes yet.
Combines all elements of an array using a reducer function, starting from the “end”, or right side, of the array.
In Array.reduceRight(fn, initial, array)
, fn
is called with
each element of the array and an accumulator, and returns
a new accumulator. The final value is the last accumulator
returned. The accumulator starts with value initial
.
Parameters:
param | type | description |
---|---|---|
fn |
(a, b) => b |
The reducer function to call on each element, where the value returned will be the next accumulator value |
initial |
b |
The initial value to use for the accumulator on the first iteration |
array |
Array<a> |
The array to iterate |
Returns:
type | description |
---|---|
b |
The final accumulator returned from fn |
Examples:
Array.reducei
Added in 0.3.0
No other changes yet.
Combines all elements of an array using a reducer function, starting from the “head”, or left side, of the array.
In Array.reducei(fn, initial, array)
, fn
is called with
an accumulator, each element of the array, and the index
of that element, and returns a new accumulator. The final
value is the last accumulator returned. The accumulator
starts with value initial
.
Parameters:
param | type | description |
---|---|---|
fn |
(a, b, Number) => a |
The reducer function to call on each element, where the value returned will be the next accumulator value |
initial |
a |
The initial value to use for the accumulator on the first iteration |
array |
Array<b> |
The array to iterate |
Returns:
type | description |
---|---|
a |
The final accumulator returned from fn |
Examples:
1 | let output = Array.reducei((acc, el, index) => { |
Array.flatMap
Added in 0.3.0
No other changes yet.
Produces a new array by calling a function 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.
Parameters:
param | type | description |
---|---|---|
fn |
b => Array<a> |
The function to be called on each element, where the value returned will be an array that gets appended to the new array |
array |
Array<b> |
The array to iterate |
Returns:
type | description |
---|---|
Array<a> |
The new array |
Throws:
InvalidArgument(String)
- When the combined length of all arrays is not an integer
Examples:
Array.every
Added in 0.3.0
No other changes yet.
Checks that the given condition is satisfied for all elements in the input array.
Parameters:
param | type | description |
---|---|---|
fn |
a => Bool |
The function to call on each element, where the returned value indicates if the element satisfies the condition |
array |
Array<a> |
The array to check |
Returns:
type | description |
---|---|
Bool |
true if all elements satisfy the condition or false otherwise |
Examples:
Array.some
Added in 0.3.0
No other changes yet.
Checks that the given condition is satisfied at least once by an element in the input array.
Parameters:
param | type | description |
---|---|---|
fn |
a => Bool |
The function to call on each element, where the returned value indicates if the element satisfies the condition |
array |
Array<a> |
The array to iterate |
Returns:
type | description |
---|---|
Bool |
true if one or more elements satisfy the condition or false otherwise |
Examples:
Array.fill
Added in 0.2.0
No other changes yet.
Replaces all elements in an array with the new value provided.
Parameters:
param | type | description |
---|---|---|
value |
a |
The value replacing each element |
array |
Array<a> |
The array to update |
Examples:
Array.fillRange
Added in 0.2.0
No other changes yet.
Replaces all elements in the provided index range in the array with the new value provided. Fails if the index is out-of-bounds.
Parameters:
param | type | description |
---|---|---|
value |
a |
The value replacing each element between the indexes |
start |
Number |
The index to begin replacement |
stop |
Number |
The (exclusive) index to end replacement |
array |
Array<a> |
The array to update |
Throws:
IndexOutOfBounds
- When the start index is out of bounds
- When the start index is greater then the stop index
Examples:
Array.reverse
Added in 0.4.0
No other changes yet.
Creates a new array with all elements in reverse order.
Parameters:
param | type | description |
---|---|---|
array |
Array<a> |
The array to reverse |
Returns:
type | description |
---|---|
Array<a> |
The new array |
Examples:
Array.toList
Added in 0.1.0
No other changes yet.
Converts the input array to a list.
Parameters:
param | type | description |
---|---|---|
array |
Array<a> |
The array to convert |
Returns:
type | description |
---|---|
List<a> |
The list containing all elements from the array |
Examples:
Array.fromList
Added in 0.1.0
No other changes yet.
Converts the input list to an array.
Parameters:
param | type | description |
---|---|---|
list |
List<a> |
The list to convert |
Returns:
type | description |
---|---|
Array<a> |
The array containing all elements from the list |
Examples:
Array.contains
Added in 0.2.0
No other changes yet.
Checks if the value is an element of the input array.
Uses the generic ==
structural equality operator.
Parameters:
param | type | description |
---|---|---|
search |
a |
The value to compare |
array |
Array<a> |
The array to inspect |
Returns:
type | description |
---|---|
Bool |
true if the value exists in the array or false otherwise |
Examples:
Array.find
Added in 0.2.0
No other changes yet.
Finds the first element in an array that satisfies the given condition.
Parameters:
param | type | description |
---|---|---|
fn |
a => Bool |
The function to call on each element, where the returned value indicates if the element satisfies the condition |
array |
Array<a> |
The array to search |
Returns:
type | description |
---|---|
Option<a> |
Some(element) containing the first value found or None otherwise |
Examples:
Array.findIndex
Added in 0.2.0
No other changes yet.
Finds the first index in an array where the element satisfies the given condition.
Parameters:
param | type | description |
---|---|---|
fn |
a => Bool |
The function to call on each element, where the returned value indicates if the element satisfies the condition |
array |
Array<a> |
The array to search |
Returns:
type | description |
---|---|
Option<Number> |
Some(index) containing the index of the first element found or None otherwise |
Examples:
Array.product
Added in 0.2.0
No other changes yet.
Combines two arrays into a Cartesian product of tuples containing
all ordered pairs (a, b)
.
Parameters:
param | type | description |
---|---|---|
array1 |
Array<a> |
The array to provide values for the first tuple element |
array2 |
Array<b> |
The array to provide values for the second tuple element |
Returns:
type | description |
---|---|
Array<(a, b)> |
The new array containing all pairs of (a, b) |
Throws:
InvalidArgument(String)
- When the multiplied array lengths are not an integer
Examples:
Array.count
Added in 0.2.0
No other changes yet.
Counts the number of elements in an array that satisfy the given condition.
Parameters:
param | type | description |
---|---|---|
fn |
a => Bool |
The function to call on each element, where the returned value indicates if the element satisfies the condition |
array |
Array<a> |
The array to iterate |
Returns:
type | description |
---|---|
Number |
The total number of elements that satisfy the condition |
Examples:
Array.counti
Added in 0.3.0
No other changes yet.
Counts the number of elements in an array that satisfy the given condition. Also passes the index to the function.
Parameters:
param | type | description |
---|---|---|
fn |
(a, Number) => Bool |
The function to call on each element, where the returned value indicates if the element satisfies the condition |
array |
Array<a> |
The array to iterate |
Returns:
type | description |
---|---|
Number |
The total number of elements that satisfy the condition |
Examples:
Array.filter
Added in 0.3.0
No other changes yet.
Produces a new array by calling a function on each element of the input array and only including it in the result array if the element satisfies the condition.
Parameters:
param | type | description |
---|---|---|
fn |
a => Bool |
The function to call on each element, where the returned value indicates if the element satisfies the condition |
array |
Array<a> |
The array to iterate |
Returns:
type | description |
---|---|
Array<a> |
The new array containing elements where fn returned true |
Examples:
Array.filteri
Added in 0.3.0
No other changes yet.
Produces a new array by calling a function on each element of the input array and only including it in the result array if the element satisfies the condition. Also passes the index to the function.
Parameters:
param | type | description |
---|---|---|
fn |
(a, Number) => Bool |
The function to call on each element, where the returned value indicates if the element satisfies the condition |
array |
Array<a> |
The array to iterate |
Returns:
type | description |
---|---|
Array<a> |
The new array containing elements where fn returned true |
Examples:
Array.unique
Added in 0.3.0
No other changes yet.
Produces a new array with any duplicates removed.
Uses the generic ==
structural equality operator.
Parameters:
param | type | description |
---|---|---|
array |
Array<a> |
The array to filter |
Returns:
type | description |
---|---|
Array<a> |
The new array with only unique values |
Examples:
Array.zip
Added in 0.4.0
version | changes |
---|---|
0.6.0 | Support zipping arrays of different sizes |
Produces a new array filled with tuples of elements from both given arrays. The first tuple will contain the first item of each array, the second tuple will contain the second item of each array, and so on.
Parameters:
param | type | description |
---|---|---|
array1 |
Array<a> |
The array to provide values for the first tuple element |
array2 |
Array<b> |
The array to provide values for the second tuple element |
Returns:
type | description |
---|---|
Array<(a, b)> |
The new array containing indexed pairs of (a, b) |
Throws:
IndexOutOfBounds
- When the arrays have different sizes
Examples:
Array.zipWith
Added in 0.5.3
No other changes yet.
Produces a new array filled with elements defined by applying a function on pairs from both given arrays. The first element will contain the result of applying the function to the first elements of each array, the second element will contain the result of applying the function to the second elements of each array, and so on.
Calling this function with arrays of different sizes will cause the returned array to have the length of the smaller array.
Parameters:
param | type | description |
---|---|---|
fn |
(a, b) => c |
The function to apply to pairs of elements |
array1 |
Array<a> |
The array whose elements will each be passed to the function as the first argument |
array2 |
Array<b> |
The array whose elements will each be passed to the function as the second argument |
Returns:
type | description |
---|---|
Array<c> |
The new array containing elements derived from applying the function to pairs of input array elements |
Throws:
IndexOutOfBounds
- When the arrays have different sizes
Examples:
Array.unzip
Added in 0.4.0
No other changes yet.
Produces two arrays by splitting apart an array of tuples.
Parameters:
param | type | description |
---|---|---|
array |
Array<(a, b)> |
The array of tuples to split |
Returns:
type | description |
---|---|
(Array<a>, Array<b>) |
An array containing all elements from the first tuple element, and an array containing all elements from the second tuple element |
Examples:
Array.join
Added in 0.4.0
No other changes yet.
Concatenates an array of strings into a single string, separated by a separator string.
Parameters:
param | type | description |
---|---|---|
separator |
String |
The separator to insert between items in the string |
items |
Array<String> |
The input strings |
Returns:
type | description |
---|---|
String |
The concatenated string |
Examples:
Array.slice
Added in 0.4.0
version | changes |
---|---|
0.6.0 | Default `end` to the Array length |
Slices an array given zero-based start and end indexes. The value at the end index will not be included in the result.
If either index is a negative number, it will be treated as a reverse index from
the end of the array. e.g. slice(1, -1, [> 'a', 'b', 'c']) == [> 'b']
.
Parameters:
param | type | description |
---|---|---|
start |
Number |
The index of the array where the slice will begin (inclusive) |
?end |
Number |
The index of the array where the slice will end (exclusive) |
array |
Array<a> |
The array to be sliced |
Returns:
type | description |
---|---|
Array<a> |
The subset of the array that was sliced |
Examples:
Array.sort
Added in 0.4.5
version | changes |
---|---|
0.6.0 | Made `compare` a default argument |
Sorts an array in-place.
Ordering is calculated using a comparator function which takes two array elements and must return 0 if both are equal, a positive number if the first is greater, and a negative number if the first is smaller.
Parameters:
param | type | description |
---|---|---|
?compare |
(num1: a, num2: a) => Number |
The comparator function used to indicate sort order |
array |
Array<a> |
The array to be sorted |
Examples:
Array.rotate
Added in 0.4.5
version | changes |
---|---|
0.6.0 | Behavior changed from right-rotation to left-rotation |
Rotates array elements in place by the specified amount to the left, such
that the n
th element becomes the first in the array.
If value is negative, array elements will be rotated by the specified amount to the right. See examples.
Parameters:
param | type | description |
---|---|---|
n |
Number |
The number of elements to rotate by |
arr |
Array<a> |
The array to be rotated |
Examples:
Array.chunk
Added in 0.6.0
No other changes yet.
Splits the given array into chunks of the provided size. If the array cannot be split evenly, the final chunk will contain the remaining elements.
Parameters:
param | type | description |
---|---|---|
chunkSize |
Number |
The maximum size of each chunk |
arr |
Array<a> |
The array to chunk |
Returns:
type | description |
---|---|
Array<Array<a>> |
An array of chunks |
Throws:
InvalidArgument(String)
- When
chunkSize
is not an integer - When
chunkSize
is less than one
Examples:
Array.Immutable
An immutable array implementation.
Added in 0.6.0
version | changes |
---|---|
0.5.4 | Originally in `"immutablearray"` module |
Types
Type declarations included in the Array.Immutable module.
Array.Immutable.ImmutableArray
Values
Functions and constants included in the Array.Immutable module.
Array.Immutable.empty
Added in 0.6.0
version | changes |
---|---|
0.5.4 | Originally in `"immutablearray"` module |
An empty array.
Examples:
Array.Immutable.isEmpty
Added in 0.6.0
version | changes |
---|---|
0.5.4 | Originally in `"immutablearray"` module |
Determines if the array contains no elements.
Parameters:
param | type | description |
---|---|---|
array |
ImmutableArray<a> |
The array to check |
Returns:
type | description |
---|---|
Bool |
true if the array is empty and false otherwise |
Examples:
Array.Immutable.length
Added in 0.6.0
version | changes |
---|---|
0.5.4 | Originally in `"immutablearray"` module |
Provides the length of the input array.
Parameters:
param | type | description |
---|---|---|
array |
ImmutableArray<a> |
The array to inspect |
Returns:
type | description |
---|---|
Number |
The number of elements in the array |
Examples:
Array.Immutable.get
Added in 0.6.0
version | changes |
---|---|
0.5.4 | Originally in `"immutablearray"` module |
Retrieves the element from the array at the specified index. A negative index is treated as an offset from the end of the array.
Parameters:
param | type | description |
---|---|---|
index |
Number |
The index to access |
array |
ImmutableArray<a> |
The array to access |
Returns:
type | description |
---|---|
a |
The element from the array |
Throws:
IndexOutOfBounds
- When the index being accessed is outside the array’s bounds
Examples:
Array.Immutable.set
Added in 0.6.0
version | changes |
---|---|
0.5.4 | Originally in `"immutablearray"` module |
Creates a new array in which the element at the specified index is set to a new value. A negative index is treated as an offset from the end of the array.
Parameters:
param | type | description |
---|---|---|
index |
Number |
The index to update |
value |
a |
The value to store |
array |
ImmutableArray<a> |
The array to update |
Returns:
type | description |
---|---|
ImmutableArray<a> |
A new array containing the new element at the given index |
Throws:
IndexOutOfBounds
- When the index being updated is outside the array’s bounds
Examples:
Array.Immutable.append
Added in 0.6.0
version | changes |
---|---|
0.5.4 | Originally in `"immutablearray"` module |
Creates a new array with the elements of the first array followed by the elements of the second array.
Parameters:
param | type | description |
---|---|---|
array1 |
ImmutableArray<a> |
The array containing elements to appear first |
array2 |
ImmutableArray<a> |
The array containing elements to appear second |
Returns:
type | description |
---|---|
ImmutableArray<a> |
The new array containing elements from array1 followed by elements from array2 |
Examples:
Array.Immutable.concat
Added in 0.6.0
version | changes |
---|---|
0.5.4 | Originally in `"immutablearray"` module |
Creates a single array containing the elements of all arrays in the provided list.
Parameters:
param | type | description |
---|---|---|
arrays |
List<ImmutableArray<a>> |
A list containing all arrays to combine |
Returns:
type | description |
---|---|
ImmutableArray<a> |
The new array |
Examples:
Array.Immutable.init
Added in 0.6.0
version | changes |
---|---|
0.5.4 | Originally in `"immutablearray"` module |
Creates a new array of the specified length where each element is initialized with the result of an initializer function. The initializer is called with the index of each array element.
Parameters:
param | type | description |
---|---|---|
length |
Number |
The length of the new array |
fn |
Number => a |
The initializer function to call with each index, where the value returned will be used to initialize the element |
Returns:
type | description |
---|---|
ImmutableArray<a> |
The new array |
Examples:
1 | use Array.{ module Immutable } |
1 | use Array.{ module Immutable } |
Array.Immutable.make
Added in 0.6.0
version | changes |
---|---|
0.5.4 | Originally in `"immutablearray"` module |
Creates a new array of the specified length with each element being initialized with the given value.
Parameters:
param | type | description |
---|---|---|
length |
Number |
The length of the new array |
value |
a |
The value to store at each index |
Returns:
type | description |
---|---|
ImmutableArray<a> |
The new array |
Examples:
1 | use Array.{ module Immutable } |
Array.Immutable.forEach
Added in 0.6.0
version | changes |
---|---|
0.5.4 | Originally in `"immutablearray"` module |
Iterates an array, calling an iterator function on each element.
Parameters:
param | type | description |
---|---|---|
fn |
a => Void |
The iterator function to call with each element |
array |
ImmutableArray<a> |
The array to iterate |
Examples:
1 | use Array.{ module Immutable } |
Array.Immutable.cycle
Added in 0.6.0
version | changes |
---|---|
0.5.4 | Originally in `"immutablearray"` module |
Iterates an array a given number of times, calling an iterator function on each element.
Parameters:
param | type | description |
---|---|---|
fn |
a => Void |
The iterator function to call with each element |
n |
Number |
The number of times to iterate the given array |
array |
ImmutableArray<a> |
The array to iterate |
Examples:
1 | use Array.{ module Immutable } |
Array.Immutable.map
Added in 0.6.0
version | changes |
---|---|
0.5.4 | Originally in `"immutablearray"` module |
Produces a new array initialized with the results of a mapper function called on each element of the input array.
Parameters:
param | type | description |
---|---|---|
fn |
a => b |
The mapper function to call on each element, where the value returned will be used to initialize the element in the new array |
array |
ImmutableArray<a> |
The array to iterate |
Returns:
type | description |
---|---|
ImmutableArray<b> |
The new array with mapped values |
Examples:
1 | use Array.{ module Immutable } |
Array.Immutable.reduce
Added in 0.6.0
version | changes |
---|---|
0.5.4 | Originally in `"immutablearray"` module |
Combines all elements of an array using a reducer function, starting from the “head”, or left side, of the array.
In ImmutableArray.reduce(fn, initial, array)
, fn
is called with
an accumulator and each element of the array, and returns
a new accumulator. The final value is the last accumulator
returned. The accumulator starts with value initial
.
Parameters:
param | type | description |
---|---|---|
fn |
(a, b) => a |
The reducer function to call on each element, where the value returned will be the next accumulator value |
initial |
a |
The initial value to use for the accumulator on the first iteration |
array |
ImmutableArray<b> |
The array to iterate |
Returns:
type | description |
---|---|
a |
The final accumulator returned from fn |
Examples:
1 | use Array.{ module Immutable } |
Array.Immutable.reduceRight
Added in 0.6.0
version | changes |
---|---|
0.5.4 | Originally in `"immutablearray"` module |
Combines all elements of an array using a reducer function, starting from the “end”, or right side, of the array.
In ImmutableArray.reduceRight(fn, initial, array)
, fn
is called with
each element of the array and an accumulator, and returns
a new accumulator. The final value is the last accumulator
returned. The accumulator starts with value initial
.
Parameters:
param | type | description |
---|---|---|
fn |
(a, b) => b |
The reducer function to call on each element, where the value returned will be the next accumulator value |
initial |
b |
The initial value to use for the accumulator on the first iteration |
array |
ImmutableArray<a> |
The array to iterate |
Returns:
type | description |
---|---|
b |
The final accumulator returned from fn |
Examples:
1 | use Array.{ module Immutable } |
Array.Immutable.flatMap
Added in 0.6.0
version | changes |
---|---|
0.5.4 | Originally in `"immutablearray"` module |
Produces a new array by calling a function 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.
Parameters:
param | type | description |
---|---|---|
fn |
a => ImmutableArray<b> |
The function to be called on each element, where the value returned will be an array that gets appended to the new array |
array |
ImmutableArray<a> |
The array to iterate |
Returns:
type | description |
---|---|
ImmutableArray<b> |
The new array |
Examples:
1 | use Array.{ module Immutable } |
Array.Immutable.fromList
Added in 0.6.0
version | changes |
---|---|
0.5.4 | Originally in `"immutablearray"` module |
Converts the input list to an array.
Parameters:
param | type | description |
---|---|---|
list |
List<a> |
The list to convert |
Returns:
type | description |
---|---|
ImmutableArray<a> |
The array containing all elements from the list |
Examples:
1 | use Array.{ module Immutable } |
Array.Immutable.toList
Added in 0.6.0
version | changes |
---|---|
0.5.4 | Originally in `"immutablearray"` module |
Converts the input array to a list.
Parameters:
param | type | description |
---|---|---|
array |
ImmutableArray<a> |
The array to convert |
Returns:
type | description |
---|---|
List<a> |
The list containing all elements from the array |
Examples:
1 | use Array.{ module Immutable } |
Array.Immutable.filter
Added in 0.6.0
version | changes |
---|---|
0.5.4 | Originally in `"immutablearray"` module |
Produces a new array by calling a function on each element of the input array and only including it in the result array if the element satisfies the condition.
Parameters:
param | type | description |
---|---|---|
fn |
a => Bool |
The function to call on each element, where the returned value indicates if the element satisfies the condition |
array |
ImmutableArray<a> |
The array to iterate |
Returns:
type | description |
---|---|
ImmutableArray<a> |
The new array containing elements where fn returned true |
Examples:
1 | use Array.{ module Immutable } |
Array.Immutable.every
Added in 0.6.0
version | changes |
---|---|
0.5.4 | Originally in `"immutablearray"` module |
Checks that the given condition is satisfied for all elements in the input array.
Parameters:
param | type | description |
---|---|---|
fn |
a => Bool |
The function to call on each element, where the returned value indicates if the element satisfies the condition |
array |
ImmutableArray<a> |
The array to check |
Returns:
type | description |
---|---|
Bool |
true if all elements satify the condition or false otherwise |
Examples:
1 | use Array.{ module Immutable } |
1 | use Array.{ module Immutable } |
Array.Immutable.some
Added in 0.6.0
version | changes |
---|---|
0.5.4 | Originally in `"immutablearray"` module |
Checks that the given condition is satisfied at least once by an element in the input array.
Parameters:
param | type | description |
---|---|---|
fn |
a => Bool |
The function to call on each element, where the returned value indicates if the element satisfies the condition |
array |
ImmutableArray<a> |
The array to iterate |
Returns:
type | description |
---|---|
Bool |
true if one or more elements satisfy the condition or false otherwise |
Examples:
1 | use Array.{ module Immutable } |
1 | use Array.{ module Immutable } |
Array.Immutable.reverse
Added in 0.6.0
version | changes |
---|---|
0.5.4 | Originally in `"immutablearray"` module |
Creates a new array with all elements in reverse order.
Parameters:
param | type | description |
---|---|---|
array |
ImmutableArray<a> |
The array to reverse |
Returns:
type | description |
---|---|
ImmutableArray<a> |
The new array |
Examples:
1 | use Array.{ module Immutable } |
Array.Immutable.contains
Added in 0.6.0
version | changes |
---|---|
0.5.4 | Originally in `"immutablearray"` module |
Checks if the value is an element of the input array.
Uses the generic ==
structural equality operator.
Parameters:
param | type | description |
---|---|---|
search |
a |
The value to compare |
array |
ImmutableArray<a> |
The array to inspect |
Returns:
type | description |
---|---|
Bool |
true if the value exists in the array or false otherwise |
Examples:
1 | use Array.{ module Immutable } |
1 | use Array.{ module Immutable } |
Array.Immutable.find
Added in 0.6.0
version | changes |
---|---|
0.5.4 | Originally in `"immutablearray"` module |
Finds the first element in an array that satisfies the given condition.
Parameters:
param | type | description |
---|---|---|
fn |
a => Bool |
The function to call on each element, where the returned value indicates if the element satisfies the condition |
array |
ImmutableArray<a> |
The array to search |
Returns:
type | description |
---|---|
Option<a> |
Some(element) containing the first value found or None otherwise |
Examples:
1 | use Array.{ module Immutable } |
1 | use Array.{ module Immutable } |
Array.Immutable.findIndex
Added in 0.6.0
version | changes |
---|---|
0.5.4 | Originally in `"immutablearray"` module |
Finds the first index in an array where the element satisfies the given condition.
Parameters:
param | type | description |
---|---|---|
fn |
a => Bool |
The function to call on each element, where the returned value indicates if the element satisfies the condition |
array |
ImmutableArray<a> |
The array to search |
Returns:
type | description |
---|---|
Option<Number> |
Some(index) containing the index of the first element found or None otherwise |
Examples:
1 | use Array.{ module Immutable } |
1 | use Array.{ module Immutable } |
Array.Immutable.product
Added in 0.6.0
version | changes |
---|---|
0.5.4 | Originally in `"immutablearray"` module |
Combines two arrays into a Cartesian product of tuples containing
all ordered pairs (a, b)
.
Parameters:
param | type | description |
---|---|---|
array1 |
ImmutableArray<a> |
The array to provide values for the first tuple element |
array2 |
ImmutableArray<b> |
The array to provide values for the second tuple element |
Returns:
type | description |
---|---|
ImmutableArray<(a, b)> |
The new array containing all pairs of (a, b) |
Examples:
Array.Immutable.count
Added in 0.6.0
version | changes |
---|---|
0.5.4 | Originally in `"immutablearray"` module |
Counts the number of elements in an array that satisfy the given condition.
Parameters:
param | type | description |
---|---|---|
fn |
a => Bool |
The function to call on each element, where the returned value indicates if the element satisfies the condition |
array |
ImmutableArray<a> |
The array to iterate |
Returns:
type | description |
---|---|
Number |
The total number of elements that satisfy the condition |
Examples:
1 | use Array.{ module Immutable } |
Array.Immutable.unique
Added in 0.6.0
version | changes |
---|---|
0.5.4 | Originally in `"immutablearray"` module |
Produces a new array with any duplicates removed.
Uses the generic ==
structural equality operator.
Parameters:
param | type | description |
---|---|---|
array |
ImmutableArray<a> |
The array to filter |
Returns:
type | description |
---|---|
ImmutableArray<a> |
The new array with only unique values |
Examples:
1 | use Array.{ module Immutable } |
Array.Immutable.zip
Added in 0.6.0
version | changes |
---|---|
0.5.4 | Originally in `"immutablearray"` module |
Produces a new array filled with tuples of elements from both given arrays. The first tuple will contain the first item of each array, the second tuple will contain the second item of each array, and so on.
Calling this function with arrays of different sizes will cause the returned array to have the length of the smaller array.
Parameters:
param | type | description |
---|---|---|
array1 |
ImmutableArray<a> |
The array to provide values for the first tuple element |
array2 |
ImmutableArray<b> |
The array to provide values for the second tuple element |
Returns:
type | description |
---|---|
ImmutableArray<(a, b)> |
The new array containing indexed pairs of (a, b) |
Examples:
1 | use Array.{ module Immutable } |
Array.Immutable.zipWith
Added in 0.6.0
version | changes |
---|---|
0.5.4 | Originally in `"immutablearray"` module |
1 | zipWith : |
Produces a new array filled with elements defined by applying a function on pairs from both given arrays. The first element will contain the result of applying the function to the first elements of each array, the second element will contain the result of applying the function to the second elements of each array, and so on.
Calling this function with arrays of different sizes will cause the returned array to have the length of the smaller array.
Parameters:
param | type | description |
---|---|---|
fn |
(a, b) => c |
The function to apply to pairs of elements |
array1 |
ImmutableArray<a> |
The array whose elements will each be passed to the function as the first argument |
array2 |
ImmutableArray<b> |
The array whose elements will each be passed to the function as the second argument |
Returns:
type | description |
---|---|
ImmutableArray<c> |
The new array containing elements derived from applying the function to pairs of input array elements |
Examples:
Array.Immutable.unzip
Added in 0.6.0
version | changes |
---|---|
0.5.4 | Originally in `"immutablearray"` module |
Produces two arrays by splitting apart an array of tuples.
Parameters:
param | type | description |
---|---|---|
array |
ImmutableArray<(a, b)> |
The array of tuples to split |
Returns:
type | description |
---|---|
(ImmutableArray<a>, ImmutableArray<b>) |
An array containing all elements from the first tuple element and an array containing all elements from the second tuple element |
Examples:
Array.Immutable.join
Added in 0.6.0
version | changes |
---|---|
0.5.4 | Originally in `"immutablearray"` module |
Concatenates an array of strings into a single string, separated by a separator string.
Parameters:
param | type | description |
---|---|---|
separator |
String |
The separator to insert between items in the string |
array |
ImmutableArray<String> |
The input strings |
Returns:
type | description |
---|---|
String |
The concatenated string |
Examples:
1 | use Array.{ module Immutable } |
Array.Immutable.slice
Added in 0.6.0
version | changes |
---|---|
0.5.4 | Originally in `"immutablearray"` module |
0.6.0 | Default `end` to the Array length |
Slices an array given zero-based start and end indexes. The value at the end index will not be included in the result.
If either index is a negative number, it will be treated as a reverse index from the end of the array.
Parameters:
param | type | description |
---|---|---|
start |
Number |
The index of the array where the slice will begin (inclusive) |
?end |
Number |
The index of the array where the slice will end (exclusive) |
array |
ImmutableArray<a> |
The array to be sliced |
Returns:
type | description |
---|---|
ImmutableArray<a> |
The subset of the array that was sliced |
Examples:
1 | use Array.{ module Immutable } |
1 | use Array.{ module Immutable } |
Array.Immutable.sort
Added in 0.6.0
version | changes |
---|---|
0.5.4 | Originally in `"immutablearray"` module, with `compare` being a required argument |
Sorts the given array based on a given comparator function.
Ordering is calculated using a comparator function which takes two array elements and must return 0 if both are equal, a positive number if the first is greater, and a negative number if the first is smaller.
Parameters:
param | type | description |
---|---|---|
?compare |
(num1: a, num2: a) => Number |
The comparator function used to indicate sort order |
array |
ImmutableArray<a> |
The array to be sorted |
Returns:
type | description |
---|---|
ImmutableArray<a> |
The sorted array |
Examples:
1 | use Array.{ module Immutable } |
Array.Immutable.rotate
Added in 0.6.0
version | changes |
---|---|
0.5.4 | Originally in `"immutablearray"` module |
Rotates array elements by the specified amount to the left, such that the
n
th element is the first in the new array.
If value is negative, array elements will be rotated by the specified amount to the right. See examples.
Parameters:
param | type | description |
---|---|---|
n |
Number |
The number of elements to rotate by |
array |
ImmutableArray<a> |
The array to be rotated |
Examples: