Skip to content

std::vectors

Vector<T>: an immutable sequence with fast lookup by index and fast append at the end. Use a List<T> for recursion over a head and a tail and for cheap prepends; use a Vector<T> when looking up by index and appending at the end are the common operations. Every Iter function works on a Vector, and Iter.to_vector collects any sequence into one.

Import with import std::vectors.

type Vector<T>

An immutable sequence with fast lookup by index. Write one as Vector[1, 2, 3], or as a list literal where a Vector is expected.

fn length<T>(vector: Vector<T>) -> NonNegativeInt

type function on Vector

The number of elements. The vector stores its count, so this does not walk the elements.

assert Vector.length(["a", "b"]) == 2
fn get<T>(vector: Vector<T>, index: Int) -> Maybe<T>

type function on Vector

Returns the element at zero-based index, or None when the index is outside the vector. Use it when the index comes from data and may miss; for an index that must exist, Vector.get(vector, index)! returns the element and traps on any other index.

assert Vector.get(["a", "b"], 1) == Some("b")
assert Vector.get(["a", "b"], 9) == None
assert Vector.get(["a", "b"], -1) == None
assert Vector.get(["a", "b"], 1)! == "b"
fn push<T>(vector: Vector<T>, item: T) -> Vector<T>

type function on Vector

Returns a new vector with item appended to the end. O(log32 n).

assert Vector.push([1, 2], 3) == [1, 2, 3]
fn set<T>(vector: Vector<T>, index: Int, item: T) -> Maybe<Vector<T>>

type function on Vector

Some of a new vector with item replacing the element at zero-based index, in O(log32 n), or None when the index is outside the vector. For an index the code knows is in range, Vector.set(vector, index, item)! returns the new vector and traps on any other index.

assert Vector.set(["a", "b"], 1, "z") == Some(["a", "z"])
assert Vector.set(["a", "b"], 2, "z") == None
assert Vector.set(["a", "b"], 0, "z")! == ["z", "b"]
fn concat<T>(a: Vector<T>, b: Vector<T>) -> Vector<T>

type function on Vector

Concatenates two vectors, in time linear in the length of b.

assert Vector.concat([1, 2], [3]) == [1, 2, 3]
fn next_item<T>(vector: Vector<T>) -> Maybe<(T, Vector<T>)>

type function on Vector

Returns the next element and remaining vector, or None if empty. Drives the Iter implementation for Vector.

assert Vector.next_item([1, 2]) == Some((1, [2]))
assert Vector.next_item([]) == None

impl Iter for Vector<T>

fn each_while(vector: Vector<T>, yield: (T) -> Bool) -> Bool

impl Iter.each_while

impl Iter for Vector<T>

fn known_count(vector: Vector<T>) -> Maybe<Int>

impl Iter.known_count

impl Contains for Vector<T>

fn contains(c: Vector<T>, x: T) -> Bool

impl Contains.contains

True if an element of the vector is == to x. A linear scan that the runtime runs, comparing each element as == does; convert to a Set for repeated lookups.

assert Vector.contains(Vector[1, 2, 3], 2)
refute Vector.contains(Vector[1, 2, 3], 9)
empty: Vector<Int> = []
refute Vector.contains(empty, 1)

impl Add<Vector<T>, Vector<T>> for Vector<T>

fn add(lhs: Vector<T>, rhs: Vector<T>) -> Vector<T>

impl Add.add

a + b is Vector.concat(a, b).

assert Vector[1, 2] + Vector[3] == [1, 2, 3]
assert Vector["a"] + Vector[] == ["a"]

impl Summable for Vector<T>

fn empty_sum() -> Vector<T>

impl Summable.empty_sum

The sum of no Vectors: the empty Vector. Summing Vectors concatenates them in order.

none: Vector<Int> = Vector.empty_sum()
assert Vector.length(none) == 0
assert Vector[1, 2] + Vector.empty_sum() == [1, 2]
assert Iter.sum([Vector[1], Vector[2, 3]]) == [1, 2, 3]

impl Display for Vector<T>

fn to_string(vector: Vector<T>) -> String

impl Display.to_string

impl Debug for Vector<T>

fn inspect(vector: Vector<T>) -> String

impl Debug.inspect

impl Equatable for Vector<T>

fn equal(a: Vector<T>, b: Vector<T>) -> Bool

impl Equatable.equal

impl Hashable for Vector<T>

fn hash(vector: Vector<T>) -> Int

impl Hashable.hash

impl Comparable for Vector<T>

fn compare(a: Vector<T>, b: Vector<T>) -> Ordering

impl Comparable.compare

subtype NonEmptyVector<T> Vector<T> when fn vector -> Vector.length(vector) > 0

A Vector with at least one element, so its first, last, largest and smallest elements are values rather than Maybes. A literal is checked when the program is compiled; NonEmptyVector.try_from(v) checks a Vector computed at run time, and NonEmptyVector.push(v, item) builds one from any Vector. A NonEmptyVector is a Vector and passes wherever one is expected. It is in the prelude.

subtype: NonEmptyVector is a subtype of Vector<T>. A literal where a NonEmptyVector is expected is checked when the program is compiled; NonEmptyVector.try_from(...) checks a value when it runs.

v: NonEmptyVector<Int> = [4, 8, 2]
assert NonEmptyVector.last(v) == 2
assert NonEmptyVector.min(v) == 2
assert Vector.get(v, 1) == Some(8)

none: Vector<Int> = []
assert NonEmptyVector.try_from(none) == None
fn of<T>(first: T, rest: Vector<T>) -> NonEmptyVector<T>

type function on NonEmptyVector

A NonEmptyVector of first followed by the elements of rest, which may be empty.

assert NonEmptyVector.of(1, [2, 3]) == Vector[1, 2, 3]
fn first<T>(vector: NonEmptyVector<T>) -> T

type function on NonEmptyVector

The first element.

assert NonEmptyVector.first([4, 5]) == 4
fn last<T>(vector: NonEmptyVector<T>) -> T

type function on NonEmptyVector

The last element.

assert NonEmptyVector.last([4, 5]) == 5
fn length<T>(vector: NonEmptyVector<T>) -> PositiveInt

type function on NonEmptyVector

The number of elements, at least one.

assert NonEmptyVector.length(["a", "b"]) == 2
fn max<T>(vector: NonEmptyVector<T>) -> T where T: Comparable

type function on NonEmptyVector

The largest element; the first of several equal ones.

assert NonEmptyVector.max([3, 9, 4]) == 9
fn min<T>(vector: NonEmptyVector<T>) -> T where T: Comparable

type function on NonEmptyVector

The smallest element; the first of several equal ones.

assert NonEmptyVector.min([3, 9, 4]) == 3
fn mean<T, R>(vector: NonEmptyVector<T>) -> R where T: Averageable<R>

type function on NonEmptyVector

The arithmetic mean of the elements, rounded as Iter.mean rounds; a value, since there is at least one.

assert NonEmptyVector.mean([2, 4]) == 3.0
fn reduce<T>(vector: NonEmptyVector<T>, f: (T, T) -> T) -> T

type function on NonEmptyVector

Combines the elements left to right with f, starting from the first, so it needs no starting value.

assert NonEmptyVector.reduce([1, 2, 3], fn acc, n -> acc * n) == 6
fn map<T, U>(vector: NonEmptyVector<T>, f: (T) -> U) -> NonEmptyVector<U>

type function on NonEmptyVector

Applies f to each element; the result has as many elements.

assert NonEmptyVector.map([1, 2], fn n -> n + 1) == Vector[2, 3]
fn with_index<T>(vector: NonEmptyVector<T>) -> NonEmptyVector<(NonNegativeInt, T)>

type function on NonEmptyVector

Each element with its index, from 0.

assert NonEmptyVector.with_index(["a"]) == Vector[(0, "a")]
fn push<T>(vector: Vector<T>, item: T) -> NonEmptyVector<T>

type function on NonEmptyVector

The elements of vector, which may be empty, then item.

assert NonEmptyVector.push(Vector[1], 2) == Vector[1, 2]
empty: Vector<Int> = []
assert NonEmptyVector.push(empty, 2) == Vector[2]
fn concat<T>(a: NonEmptyVector<T>, b: Vector<T>) -> NonEmptyVector<T>

type function on NonEmptyVector

The elements of a, then those of b, which may be empty.

assert NonEmptyVector.concat([1], Vector[2, 3]) == Vector[1, 2, 3]
fn try_from<T>(value: Vector<T>) -> Maybe<NonEmptyVector<T>>

type function on NonEmptyVector

Some of the value when it satisfies NonEmptyVector’s rules, None when it does not.