Array
Documentation for the Array type.
A sequence of values.
You can construct an array by enclosing a comma-separated sequence of values in parentheses. The values do not have to be of the same type.
You can access and update array items with the .at() method. Indices are zero-based and negative indices wrap around to the end of the array. You can iterate over an array using a for loop. Arrays can be added together with the + operator, joined together and multiplied with integers.
Note: An array of length one needs a trailing comma, as in (1,). This is to disambiguate from a simple parenthesized expressions like (1 + 2) * 3. An empty array is written as ().
Example
Constructor
Converts a value to an array.
Note that this function is only intended for conversion of a collection-like value to an array, not for creation of an array from individual items. Use the array syntax (1, 2, 3) (or (1,) for a single-element array) instead.
#array(
value
) -> arrayParameters
Prop
Type
Methods
The number of values in the array.
Returns the first item in the array. May be used on the left-hand side an assignment. Returns the default value if the array is empty or fails with an error is no default value was specified.
#array.first(
default: any
) -> anyParameters
Prop
Type
Returns the last item in the array. May be used on the left-hand side of an assignment. Returns the default value if the array is empty or fails with an error is no default value was specified.
#array.last(
default: any
) -> anyParameters
Prop
Type
Returns the item at the specified index in the array. May be used on the left-hand side of an assignment. Returns the default value if the index is out of bounds or fails with an error if no default value was specified.
#array.at(
index,
default: any
) -> anyParameters
Prop
Type
Adds a value to the end of the array.
#array.push(
value
) -> Parameters
Prop
Type
Removes the last item from the array and returns it. Fails with an error if the array is empty.
Inserts a value into the array at the specified index, shifting all subsequent elements to the right. Fails with an error if the index is out of bounds.
To replace an element of an array, use at.
#array.insert(
index,
value
) -> Parameters
Prop
Type
Removes the value at the specified index from the array and return it.
#array.remove(
index,
default: any
) -> anyParameters
Prop
Type
Extracts a subslice of the array. Fails with an error if the start or end index is out of bounds.
#array.slice(
start,
end,
count: int
) -> arrayParameters
Prop
Type
Whether the array contains the specified value.
This method also has dedicated syntax: You can write 2 in (1, 2, 3) instead of (1, 2, 3).contains(2).
#array.contains(
value
) -> boolParameters
Prop
Type
Searches for an item for which the given function returns true and returns the first match or none if there is no match.
#array.find(
searcher
) -> any noneParameters
Prop
Type
Searches for an item for which the given function returns true and returns the index of the first match or none if there is no match.
#array.position(
searcher
) -> none intParameters
Prop
Type
Create an array consisting of a sequence of numbers.
If you pass just one positional parameter, it is interpreted as the end of the range. If you pass two, they describe the start and end of the range.
This function is available both in the array function's scope and globally.
#array.range(
start,
end,
step: int
) -> arrayParameters
Prop
Type
Produces a new array with only the items from the original one for which the given function returns true.
#array.filter(
test
) -> arrayParameters
Prop
Type
Produces a new array in which all items from the original one were transformed with the given function.
#array.map(
mapper
) -> arrayParameters
Prop
Type
Returns a new array with the values alongside their indices.
The returned array consists of (index, value) pairs in the form of length-2 arrays. These can be destructured with a let binding or for loop.
#array.enumerate(
start: int
) -> arrayParameters
Prop
Type
Zips the array with other arrays.
Returns an array of arrays, where the ith inner array contains all the ith elements from each original array.
If the arrays to be zipped have different lengths, they are zipped up to the last element of the shortest array and all remaining elements are ignored.
This function is variadic, meaning that you can zip multiple arrays together at once: (1, 2).zip(("A", "B"), (10, 20)) yields ((1, "A", 10), (2, "B", 20)).
#array.zip(
exact: bool,
others
) -> arrayParameters
Prop
Type
Folds all items into a single value using an accumulator function.
#array.fold(
init,
folder
) -> anyParameters
Prop
Type
Sums all items (works for all types that can be added).
#array.sum(
default: any
) -> anyParameters
Prop
Type
Calculates the product of all items (works for all types that can be multiplied).
#array.product(
default: any
) -> anyParameters
Prop
Type
Whether the given function returns true for any item in the array.
#array.any(
test
) -> boolParameters
Prop
Type
Whether the given function returns true for all items in the array.
#array.all(
test
) -> boolParameters
Prop
Type
Combine all nested arrays into a single flat one.
Return a new array with the same items, but in reverse order.
Split the array at occurrences of the specified value.
#array.split(
at
) -> arrayParameters
Prop
Type
Combine all items in the array into one.
#array.join(
separator,
last: any,
default: any | none
) -> anyParameters
Prop
Type
Returns an array with a copy of the separator value placed between adjacent elements.
#array.intersperse(
separator
) -> arrayParameters
Prop
Type
Splits an array into non-overlapping chunks, starting at the beginning, ending with a single remainder chunk.
All chunks but the last have chunk-size elements. If exact is set to true, the remainder is dropped if it contains less than chunk-size elements.
#array.chunks(
chunk-size,
exact: bool
) -> arrayParameters
Prop
Type
Returns sliding windows of window-size elements over an array.
If the array length is less than window-size, this will return an empty array.
#array.windows(
window-size
) -> arrayParameters
Prop
Type
Return a sorted version of this array, optionally by a given key function. The sorting algorithm used is stable.
Returns an error if a pair of values selected for comparison could not be compared, or if the key or comparison function (if given) yield an error.
To sort according to multiple criteria at once, e.g. in case of equality between some criteria, the key function can return an array. The results are in lexicographic order.
#array.sorted(
key: function,
by: function
) -> arrayParameters
Prop
Type
Deduplicates all items in the array.
Returns a new array with all duplicate items removed. Only the first element of each duplicate is kept.
#array.dedup(
key: function
) -> arrayParameters
Prop
Type
Converts an array of pairs into a dictionary. The first value of each pair is the key, the second the value.
If the same key occurs multiple times, the last value is selected.
Reduces the elements to a single one, by repeatedly applying a reducing operation.
If the array is empty, returns none, otherwise, returns the result of the reduction.
The reducing function is a closure with two arguments: an "accumulator", and an element.
For arrays with at least one element, this is the same as array.fold with the first element of the array as the initial accumulator value, folding every subsequent element into it.
#array.reduce(
reducer
) -> anyParameters
Prop
Type