TypstDocumentation

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 "))
Preview

Methods
Question mark

len method

The number of values in the array.

value.len(
) -> integer

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.

value.first(
) -> any

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.

value.last(
) -> any

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.

value.at() -> any

index
integerRequiredPositional
Question mark

The index at which to retrieve the element.

push method

Add a value to the end of the array.

value.push(
any
)

value
anyRequiredPositional
Question mark

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.

value.pop(
) -> any

insert method

Insert a value into the array at the specified index. Fails with an error if the index is out of bounds.

value.insert(
integer,any,
)

index
integerRequiredPositional
Question mark

The index at which to insert the element.

value
anyRequiredPositional
Question mark

The value to insert into the array.

remove method

Remove the value at the specified index from the array and return it.

value.remove() -> any

index
integerRequiredPositional
Question mark

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
integerRequiredPositional
Question mark

The start index (inclusive).

end
integerPositional
Question mark

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.contains(
any
) -> boolean

value
anyRequiredPositional
Question mark

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.

value.find() -> anynone

searcher
functionRequiredPositional
Question mark

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.

value.position() -> integernone

searcher
functionRequiredPositional
Question mark

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.

value.filter() -> array

test
functionRequiredPositional
Question mark

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.

value.map() -> array

mapper
functionRequiredPositional
Question mark

The function to apply to each element.

fold method

Folds all elements into a single value using an accumulator function.

value.fold() -> any

init
anyRequiredPositional
Question mark

The initial value to start with.

folder
functionRequiredPositional
Question mark

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.

value.any() -> boolean

test
functionRequiredPositional
Question mark

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.

value.all() -> boolean

test
functionRequiredPositional
Question mark

The function to apply to each element. Must return a boolean.

flatten method

Combine all nested arrays into a single flat one.

value.flatten(
) -> array

rev method

Return a new array with the same elements, but in reverse order.

value.rev(
) -> array

join method

Combine all elements in the array into one.

value.join(
any,last: any,
) -> any

separator
anyPositional
Question mark

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.

value.sorted(
) -> array