Development build 586e1bd4
Typst文档简体中文版v0.15.1 + dev 586e1bd4

array自始可用Go to source

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

构造函数
Question mark
0.7.0起可用Go to source

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.

Expand展开示例
#let hi = "Hello 😃" #array(bytes(hi))

value
bytes or array or version
必需参数
Question mark
位置参数
Question mark

The value that should be converted to an array.

定义
Question mark

len自始可用Go to source

The number of values in the array.

self.len() → int

first自始可用Go to source

Returns the first 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 if no default value was specified.

self.first(default: any) → any

default
any

A default value to return if the array is empty.

last自始可用Go to source

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 if no default value was specified.

self.last(default: any) → any

default
any

A default value to return if the array is empty.

at自始可用Go to source

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.

self.at(
int,default: any,
) → any

index
int
必需参数
Question mark
位置参数
Question mark

The index at which to retrieve the item. If negative, indexes from the back.

default
any

A default value to return if the index is out of bounds.

push自始可用Go to source

Adds a value to the end of the array.

self.push(any) → none

value
any
必需参数
Question mark
位置参数
Question mark

The value to insert at the end of the array.

pop自始可用Go to source

Removes the last item from the array and returns it. Fails with an error if the array is empty.

self.pop() → any

insert自始可用Go to source

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.

self.insert(
int,any,
) → none

index
int
必需参数
Question mark
位置参数
Question mark

The index at which to insert the item. If negative, indexes from the back.

value
any
必需参数
Question mark
位置参数
Question mark

The value to insert into the array.

remove自始可用Go to source

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

self.remove(
int,default: any,
) → any

index
int
必需参数
Question mark
位置参数
Question mark

The index at which to remove the item. If negative, indexes from the back.

default
any

A default value to return if the index is out of bounds.

slice自始可用Go to source

Extracts a subslice of the array. Fails with an error if the start or end index is out of bounds.

self.slice() → array

start
int
必需参数
Question mark
位置参数
Question mark

The start index (inclusive). If negative, indexes from the back.

end
none or int
位置参数
Question mark
默认值:none

The end index (exclusive). If omitted, the whole slice until the end of the array is extracted. If negative, indexes from the back.

count

The number of items to extract. This is equivalent to passing start + count as the end position. Mutually exclusive with end.

contains自始可用Go to source

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).

self.contains(any) → bool

value
any
必需参数
Question mark
位置参数
Question mark

The value to search for.

find自始可用Go to source

Searches for an item for which the given function returns true and returns the first match or none if there is no match.

self.find(function) → noneany

searcher
function
必需参数
Question mark
位置参数
Question mark

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

position自始可用Go to source

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.

self.position(function) → noneint

searcher
function
必需参数
Question mark
位置参数
Question mark

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

Expand展开示例
#let values = (1, 7, 4, 6, 9) #values.position(x => calc.even(x)) \ // Or equivalently: #values.position(calc.even)

range自始可用Go to source

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.

Expand展开示例
#range(5) \ #range(2, 5) \ #range(20, step: 4) \ #range(21, step: 4) \ #range(5, 2, step: -1)
array.range() → array

start
int
位置参数
Question mark
默认值:0

The start of the range (inclusive).

end
int
必需参数
Question mark
位置参数
Question mark

The end of the range.

inclusive默认值:false

Whether end is inclusive.

Expand展开示例
#range(0, inclusive: true) \ #range(7, 10, inclusive: true) \ #range(-8, -4, inclusive: true) \ #range(-6, step: -2, inclusive: true)

step默认值:1

The distance between the generated numbers.

filter自始可用Go to source

Produces a new array with only the items from the original one for which the given function returns true.

self.filter(function) → array

test
function
必需参数
Question mark
位置参数
Question mark

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

map自始可用Go to source

Produces a new array in which all items from the original one were transformed with the given function.

self.map(function) → array

mapper
function
必需参数
Question mark
位置参数
Question mark

The function to apply to each item.

enumerate0.2.0起可用Go to source

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.

Expand展开示例
#for (i, value) in ("A", "B", "C").enumerate() { [#i: #value \ ] } #("A", "B", "C").enumerate(start: 1)
self.enumerate(start: int) → array

start默认值:0

The index returned for the first pair of the returned list.

zip0.3.0起可用Go to source

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)).

self.zip() → array

exact默认值:false

Whether all arrays have to have the same length. For example, (1, 2).zip((1, 2, 3), exact: true) produces an error.

others
array
必需参数
Question mark
位置参数
Question mark
变长参数
Question mark

The arrays to zip with.

fold自始可用Go to source

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

Expand展开示例
#let array = (1, 2, 3, 4) #array.fold(0, (acc, x) => acc + x)
self.fold() → any

init
any
必需参数
Question mark
位置参数
Question mark

The initial value to start with.

folder
function
必需参数
Question mark
位置参数
Question mark

The folding function. Must have two parameters: One for the accumulated value and one for an item.

sum0.3.0起可用Go to source

Sums all items (works for all types that can be added).

self.sum(default: any) → any

default
any

What to return if the array is empty. Must be set if the array can be empty.

product0.3.0起可用Go to source

Calculates the product of all items (works for all types that can be multiplied).

self.product(default: any) → any

default
any

What to return if the array is empty. Must be set if the array can be empty.

any自始可用Go to source

Whether the given function returns true for any item in the array.

self.any(function) → bool

test
function
必需参数
Question mark
位置参数
Question mark

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

all自始可用Go to source

Whether the given function returns true for all items in the array.

self.all(function) → bool

test
function
必需参数
Question mark
位置参数
Question mark

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

flatten自始可用Go to source

Combine all nested arrays into a single flat one.

self.flatten() → array

rev自始可用Go to source

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

self.rev() → array

split自始可用Go to source

Split the array at occurrences of the specified value.

Expand展开示例
#(1, 1, 2, 3, 2, 4, 5).split(2)
self.split(any) → array

at
any
必需参数
Question mark
位置参数
Question mark

The value to split at.

join自始可用Go to source

Combine all items in the array into one.

self.join(
noneany,last: any,default: noneany,
) → any

separator
none or any
位置参数
Question mark
默认值:none

A value to insert between each item of the array.

last
any

An alternative separator between the last two items.

default
none or any
默认值:none

What to return if the array is empty.

intersperse0.8.0起可用Go to source

Returns an array with a copy of the separator value placed between adjacent elements.

Expand展开示例
#("A", "B", "C").intersperse("-")
self.intersperse(any) → array

separator
any
必需参数
Question mark
位置参数
Question mark

The value that will be placed between each adjacent element.

chunks0.11.0起可用Go to source

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.

Expand展开示例
#let array = (1, 2, 3, 4, 5, 6, 7, 8) #array.chunks(3) \ #array.chunks(3, exact: true)
self.chunks() → array

chunk-size
int
必需参数
Question mark
位置参数
Question mark

How many elements each chunk may at most contain.

exact默认值:false

Whether to discard the remainder if its size is less than chunk-size.

windows0.12.0起可用Go to source

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.

Expand展开示例
#let array = (1, 2, 3, 4, 5, 6, 7, 8) #array.windows(5)
self.windows(int) → array

window-size
int
必需参数
Question mark
位置参数
Question mark

How many elements each window will contain.

sorted自始可用Go to source

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.

Expand展开示例
#let array = ( (a: 2, b: 4), (a: 1, b: 5), (a: 2, b: 3), ) #array.sorted(key: it => (it.a, it.b))
self.sorted() → array

key

If given, applies this function to each element in the array to determine the keys to sort by.

by

If given, uses this function to compare every two elements in the array.

The function will receive two elements in the array for comparison, and should return a boolean indicating their order: true indicates that the elements are in order, while false indicates that they should be swapped. To keep the sort stable, if the two elements are equal, the function should return true.

If this function does not order the elements properly (e.g., by returning false for both (x, y) and (y, x), or for (x, x)), the resulting array will be in unspecified order.

When used together with key, by will be passed the keys instead of the elements.

Expand展开示例
#( "sorted", "by", "decreasing", "length", ).sorted( key: s => s.len(), by: (l, r) => l >= r, )

dedup0.7.0起可用Go to source

Deduplicates all items in the array.

Returns a new array with all duplicate items removed. Only the first element of each duplicate is kept.

Expand展开示例
#(3, 3, 1, 2, 3).dedup()
self.dedup(key: function) → array

key

If given, applies this function to each element in the array to determine the keys to deduplicate by.

Expand展开示例
#("apple", "banana", " apple ").dedup(key: s => s.trim())

to-dict0.12.0起可用Go to source

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.

Expand展开示例
#( ("apples", 2), ("peaches", 3), ("apples", 5), ).to-dict()
self.to-dict() → dictionary

reduce0.12.0起可用Go to source

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.

Expand展开示例
#let array = (2, 1, 4, 3) #array.reduce((acc, x) => calc.max(acc, x))
self.reduce(function) → any

reducer
function
必需参数
Question mark
位置参数
Question mark

The reducing function. Must have two parameters: One for the accumulated value and one for an item.