array
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 elements 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.
Empty parenthesis yield an array of length zero and a parentheses-wrapped value with trailing comma yields an array of length one.
Example
#let values = (1, 7, 4, -3, 2)
#values.at(0) \
#(values.at(0) = 3)
#values.at(-1) \
#values.find(calc.even) \
#values.filter(calc.odd) \
#values.map(calc.abs) \
#values.rev() \
#(1, (2, 3)).flatten() \
#(("A", "B", "C")
.join(", ", last: " and "))

MethodsMethods are functions that are available on values of a type. They can be called for a value using the .
operator.
.
operator.len
method
The number of values in the array.
first
method
Returns the first element in the array. May be used on the left-hand side of an assignment. Fails with an error if the array is empty.
last
method
Returns the last element in the array. May be used on the left-hand side of an assignment. Fails with an error if the array is empty.
at
method
Returns the element at the specified index in the array. May be used on the left-hand side of an assignment. Fails with an error if the index is out of bounds.
index
The index at which to retrieve the element.
push
method
Add a value to the end of the array.
value
anyRequiredPositionalPositional parameters are specified in order, without names.
The value to insert at the end of the array.
pop
method
Remove the last element from the array and return it. Fails with an error if the array is empty.
insert
method
Insert a value into the array at the specified index. Fails with an error if the index is out of bounds.
index
The index at which to insert the element.
value
anyRequiredPositionalPositional parameters are specified in order, without names.
The value to insert into the array.
remove
method
Remove the value at the specified index from the array and return it.
index
The index at which to remove the element.
slice
method
Extract a subslice of the array. Fails with an error if the start or index is out of bounds.
start
The start index (inclusive).
end
The end index (exclusive). If omitted, the whole slice until the end of the array is extracted.
count
The number of elements to extract. This is equivalent to passing start + count
as the end
position. Mutually exclusive with end
.
contains
method
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)
.
value
anyRequiredPositionalPositional parameters are specified in order, without names.
The value to search for.
find
method
Searches for an element for which the given function returns true
and
returns the first match or none
if there is no match.
searcher
The function to apply to each element. Must return a boolean.
position
method
Searches for an element for which the given function returns true
and
returns the index of the first match or none
if there is no match.
searcher
The function to apply to each element. Must return a boolean.
filter
method
Produces a new array with only the elements from the original one for which the given function returns true.
test
The function to apply to each element. Must return a boolean.
map
method
Produces a new array in which all elements from the original one were transformed with the given function.
mapper
The function to apply to each element.
fold
method
Folds all elements into a single value using an accumulator function.
init
anyRequiredPositionalPositional parameters are specified in order, without names.
The initial value to start with.
folder
The folding function. Must have two parameters: One for the accumulated value and one for an element.
any
method
Whether the given function returns true
for any element in the array.
test
The function to apply to each element. Must return a boolean.
all
method
Whether the given function returns true
for all elements in the array.
test
The function to apply to each element. Must return a boolean.
flatten
method
Combine all nested arrays into a single flat one.
rev
method
Return a new array with the same elements, but in reverse order.
join
method
Combine all elements in the array into one.
separator
anyPositionalPositional parameters are specified in order, without names.
A value to insert between each element of the array.
last
any
An alternative separator between the last two elements
sorted
method
Return a new array with the same elements, but sorted.