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

构造函数参数参数是传给函数的输入,写在函数名后的括号中。
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.
#let hi = "Hello 😃"
#array(bytes(hi))

array()->定义定义这些函数和类型带有附属定义。要访问这种定义,请先写上函数或类型的名称,再加上定义的名称,并用句点在中间分隔。
first
firstReturns the first item in the array. May be used on the left-hand side of an assignment. Fails with an error if the array is empty.
self.first()->anylast
lastReturns the last item in the array. May be used on the left-hand side of an assignment. Fails with an error if the array is empty.
self.last()->anyat
atReturns 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.
self.at(,any)->anyindex
indexThe index at which to retrieve the item. If negative, indexes from the back.
defaultany
defaultA default value to return if the index is out of bounds.
push
pushAdds a value to the end of the array.
self.push(any)valueany必需参数必需参数必需参数在调用函数时必须传入。位置参数位置参数位置参数按顺序传入,不带参数名。
valueThe value to insert at the end of the array.
pop
popRemoves the last item from the array and returns it. Fails with an error if the array is empty.
self.pop()->anyinsert
insertInserts a value into the array at the specified index. Fails with an error if the index is out of bounds.
self.insert(,any)index
indexThe index at which to insert the item. If negative, indexes from the back.
valueany必需参数必需参数必需参数在调用函数时必须传入。位置参数位置参数位置参数按顺序传入,不带参数名。
valueThe value to insert into the array.
remove
removeRemoves the value at the specified index from the array and return it.
self.remove(,any)->anyindex
indexThe index at which to remove the item. If negative, indexes from the back.
defaultany
defaultA default value to return if the index is out of bounds.
slice
sliceExtracts a subslice of the array. Fails with an error if the start or index is out of bounds.
self.slice(,,)->start
startThe start index (inclusive). If negative, indexes from the back.
The end index (exclusive). If omitted, the whole slice until the end of the array is extracted. If negative, indexes from the back.
默认值:none
count
countThe number of items to extract. This is equivalent to passing
start + count as the end position. Mutually exclusive with end.
contains
containsWhether 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).
self.contains(any)->valueany必需参数必需参数必需参数在调用函数时必须传入。位置参数位置参数位置参数按顺序传入,不带参数名。
valueThe value to search for.
find
findSearches for an item for which the given function returns true and
returns the first match or none if there is no match.
self.find()->anynonesearcher
searcherThe function to apply to each item. Must return a boolean.
position
positionSearches 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.
self.position()->searcher
searcherThe function to apply to each item. Must return a boolean.
range
rangeCreate 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.
展开例子
#range(5) \
#range(2, 5) \
#range(20, step: 4) \
#range(21, step: 4) \
#range(5, 2, step: -1)

array.range(,,step:)->filter
filterProduces a new array with only the items from the original one for which the given function returns true.
self.filter()->test
testThe function to apply to each item. Must return a boolean.
map
mapProduces a new array in which all items from the original one were transformed with the given function.
self.map()->mapper
mapperThe function to apply to each item.
enumerate
enumerateReturns 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.
self.enumerate()->zip
zipZips 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)).
self.zip()->others
othersThe arrays to zip with.
fold
foldFolds all items into a single value using an accumulator function.
self.fold(any,)->anyinitany必需参数必需参数必需参数在调用函数时必须传入。位置参数位置参数位置参数按顺序传入,不带参数名。
initThe initial value to start with.
folder
folderThe folding function. Must have two parameters: One for the accumulated value and one for an item.
sum
sumSums all items (works for all types that can be added).
self.sum(any)->anydefaultany
defaultWhat to return if the array is empty. Must be set if the array can be empty.
product
productCalculates the product all items (works for all types that can be multiplied).
self.product(any)->anydefaultany
defaultWhat to return if the array is empty. Must be set if the array can be empty.
any
anyWhether the given function returns true for any item in the array.
self.any()->test
testThe function to apply to each item. Must return a boolean.
all
allWhether the given function returns true for all items in the array.
self.all()->test
testThe function to apply to each item. Must return a boolean.
split
splitSplit the array at occurrences of the specified value.
self.split(any)->atany必需参数必需参数必需参数在调用函数时必须传入。位置参数位置参数位置参数按顺序传入,不带参数名。
atThe value to split at.
join
joinCombine all items in the array into one.
self.join(anynone,last:any)->anylastany
lastAn alternative separator between the last two items.
intersperse
intersperseReturns an array with a copy of the separator value placed between adjacent elements.
self.intersperse(any)->separatorany必需参数必需参数必需参数在调用函数时必须传入。位置参数位置参数位置参数按顺序传入,不带参数名。
separatorThe value that will be placed between each adjacent element.
sorted
sortedReturn a sorted version of this array, optionally by a given key function. The sorting algorithm used is stable.
Returns an error if two values could not be compared or if the key function (if given) yields an error.
self.sorted(key:)->key
keyIf given, applies this function to the elements in the array to determine the keys to sort by.
dedup
dedupDeduplicates all items in the array.
Returns a new array with all duplicate items removed. Only the first element of each duplicate is kept.
展开例子
#(1, 1, 2, 3, 1).dedup()

self.dedup(key:)->key
keyIf given, applies this function to the elements in the array to determine the keys to deduplicate by.