Skip to content

std/vectors

Operations on the immutable random-access Vector<T> type. Use List<T> for cons-style recursion and cheap prepends; use Vector<T> when indexed lookup and push-at-end are the natural operations. Generic iteration goes through Iter.

Import with import std/vectors.

Types

type Vector<T>

Immutable random-access sequence of elements.

fn empty<T>(): Vector<T>

type function on Vector

Returns an empty vector.

Interactive Tests

v: Vector<Int> = Vector.empty()
assert Vector.length(v) == 0
fn from_list<T>(items: List<T>): Vector<T>

type function on Vector

Builds a vector from a list, preserving order.

Interactive Tests

assert Vector.from_list([1, 2, 3]) == #[1, 2, 3]
fn to_list<T>(vector: Vector<T>): List<T>

type function on Vector

Converts a vector to a list, preserving order.

Interactive Tests

assert Vector.to_list(#[1, 2, 3]) == [1, 2, 3]
fn length<T>(vector: Vector<T>): Int

type function on Vector

Returns the number of elements. O(1).

Interactive Tests

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

type function on Vector

Returns the element at zero-based index, or None when out of bounds.

Interactive Tests

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

type function on Vector

Returns a new vector with item appended to the end.

Interactive Tests

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

Returns a new vector with item replacing the element at index, or None when the index is out of bounds.

Interactive Tests

assert Vector.set(#["a", "b"], 1, "z") == Some(#["a", "z"])
fn concat<T>(a: Vector<T>, b: Vector<T>): Vector<T>

type function on Vector

Concatenates two vectors.

Interactive Tests

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.

Interactive Tests

assert Vector.next_item(#[1, 2]) == Some((1, #[2]))
assert Vector.next_item(#[]) == None
fn each_while(vector: Vector<T>, yield: (T) -> Bool): Bool

impl Iter.each_while

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

impl Iter.known_count

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

impl Add.add

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

impl Display.to_string

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

impl Debug.inspect

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

impl Equatable.equal?

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

impl Hashable.hash

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

impl Comparable.compare