Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

array

std › array

Array<T, N>: N values of type T, and ways to build new arrays from old ones. Every value is immutable, so each operation returns a new array.

from std.array import *

macro show_all<const N: int>(values: Array<int, N>) {
    @for v in values {
        @emit v
    }
}

macro double(x: int) -> int {
    @return x * 2
}

const values = Array<int, 3> { __el0: 10, __el1: 20, __el2: 30 }

show_all reversed(values)
show_all mapped(appended(values, 40), double)
30 20 10
20 40 60 80

An array is built with its elements named __el0, __el1, …, or by an @for inside the construction. @for v in values visits the elements in order.

Macros

get

The element at index, counting from 0. An index outside the array is a compile error.

SyntaxParametersResultDescription
get(arr, index)<T>, arr: Array<T, ...>, index: intreturns T

updated

A copy of arr with the element at index replaced by value.

SyntaxParametersResultDescription
updated(arr, index, value)<T>, arr: Array<T, ...>, index: int, value: Treturns Array<T, ...>

reversed

A copy of arr in reverse order.

SyntaxParametersResultDescription
reversed(arr)<T>, arr: Array<T, ...>returns Array<T, ...>

popped

A copy of arr without its last element. arr can’t be empty.

SyntaxParametersResultDescription
popped(arr)<T, const N: int>, arr: Array<T, N>returns Array<T, (N - 1)>

first

The first element. arr can’t be empty.

SyntaxParametersResultDescription
first(arr)<T>, arr: Array<T, ...>returns T

last

The last element. arr can’t be empty.

SyntaxParametersResultDescription
last(arr)<T>, arr: Array<T, ...>returns T

mapped

A new array holding f of each element, in order. f is any macro that takes a T and returns a U.

SyntaxParametersResultDescription
mapped(arr, f)<T, U, F: Fn(T) -> U>, arr: Array<T, ...>, f: Freturns Array<U, ...>

array_from_struct

An Array<int, N> of a struct’s __el0..__el{len - 1} fields, such as a String’s characters or a Range’s values.

SyntaxParametersResultDescription
array_from_struct source<S>, source: S

enumerate

Each element paired with its index, as an Enumerated<T>.

SyntaxParametersResultDescription
enumerate(arr)<T>, arr: Array<T, ...>returns Array<Enumerated<T>, ...>

appended

A copy of arr with value added at the end.

SyntaxParametersResultDescription
appended(arr, value)<T, const N: int>, arr: Array<T, N>, value: Treturns Array<T, (N + 1)>

Types

Array

struct Array<T, const N: int>

N values of type T, in order.

FieldTypeDescription
lenintThe number of elements, N. skip, so @for doesn’t visit it.

Some of its fields are generated by @for or @if.