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.
Exports
Section titled “Exports”Types
type Vector
Section titled “type Vector”type Vector<T>
Immutable random-access sequence of elements.
Vector.empty
Section titled “Vector.empty”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
Vector.from_list
Section titled “Vector.from_list”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]
Vector.to_list
Section titled “Vector.to_list”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]
Vector.length
Section titled “Vector.length”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
Vector.at
Section titled “Vector.at”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
Vector.push
Section titled “Vector.push”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]
Vector.set
Section titled “Vector.set”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"])
Vector.concat
Section titled “Vector.concat”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]
Vector.next_item
Section titled “Vector.next_item”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
Vector.each_while
Section titled “Vector.each_while”fn each_while(vector: Vector<T>, yield: (T) -> Bool): Bool
impl Iter.each_while
Vector.known_count
Section titled “Vector.known_count”fn known_count(vector: Vector<T>): Maybe<Int>
impl Iter.known_count
Vector.add
Section titled “Vector.add”fn add(lhs: Vector<T>, rhs: Vector<T>): Vector<T>
impl Add.add
Vector.to_string
Section titled “Vector.to_string”fn to_string(vector: Vector<T>): String
impl Display.to_string
Vector.inspect
Section titled “Vector.inspect”fn inspect(vector: Vector<T>): String
impl Debug.inspect
Vector.equal?
Section titled “Vector.equal?”fn equal?(a: Vector<T>, b: Vector<T>): Bool
impl Equatable.equal?
Vector.hash
Section titled “Vector.hash”fn hash(vector: Vector<T>): Int
impl Hashable.hash
Vector.compare
Section titled “Vector.compare”fn compare(a: Vector<T>, b: Vector<T>): Ordering
impl Comparable.compare