Skip to content

Std.Array

opaque type Array[A]

An immutable sequence of values of type A, of fixed length, read by position.

fn empty[A]() -> Array[A]

The array of no elements.

fn from_list[A](xs: List[A]) -> Array[A]

The elements in the order of xs.

fn to_list[A](a: Array[A]) -> List[A]

The elements in order.

fn repeat[A](value: A, count: Int) -> Array[A]

count copies of value. Empty if count is not positive.

fn size[A](a: Array[A]) -> Int

The number of elements.

fn is_empty[A](a: Array[A]) -> Bool

Whether there are no elements.

fn get[A](a: Array[A], index: Int) -> Option[A]

The element at index. None if out of range or negative.

fn set[A](a: Array[A], index: Int, value: A) -> Array[A]

The elements of a with the one at index replaced by value. A defect if index is out of range or negative. On WASM and C, it updates a in place when a is referred to from nowhere else.

fn slice[A](a: Array[A], from: Int, to: Int) -> Array[A]

The elements from from inclusive to to exclusive. Both are clamped to 0 to size(a); empty if from is not less than to.

fn append[A](a: Array[A], b: Array[A]) -> Array[A]

The elements of a, then those of b.

fn concat[A](parts: List[Array[A]]) -> Array[A]

The elements of all of them in order.

fn reverse[A](a: Array[A]) -> Array[A]

The elements in reverse order.

fn map[A, B](a: Array[A], f: fn(A) -> B fails E(f) uses { R(f) }) -> Array[B] fails E(f) uses { R(f) }

Applies f to each element, from the first.

fn each[A](a: Array[A], f: fn(A) -> () fails E(f) uses { R(f) }) -> () fails E(f) uses { R(f) }

Calls f on each element, from the first. Stops at the first failure.

fn fold[A, B](a: Array[A], from: B, f: fn(B, A) -> B fails E(f) uses { R(f) }) -> B fails E(f) uses { R(f) }

Folds from the first element: f(f(f(from, a0), a1), a2).

fn find_index[A](a: Array[A], f: fn(A) -> Bool fails E(f) uses { R(f) }) -> Option[Int] fails E(f) uses { R(f) }

The position of the first element that gives True.

fn sort_by[A](a: Array[A], f: fn(A, A) -> Ordering fails E(f) uses { R(f) }) -> Array[A] fails E(f) uses { R(f) }

Sorts in the order of f. Stable, with the same result as List.sort_by.

fn sort[A: Ord](a: Array[A]) -> Array[A]

Sorts in the Ord order. Stable.

fn lower_bound[A: Ord](a: Array[A], value: A) -> Int

In an array sorted in the Ord order, the smallest position whose element is not less than value: the position where value would be inserted to keep the order. size(a) if every element is less. The bisection is fixed, so the result is the same on every target even when a is not sorted.

fn binary_search[A: Ord](a: Array[A], value: A) -> Option[Int]

In an array sorted in the Ord order, the smallest position whose element is equal to value. None if there is none.