std/vector
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 still
goes through the Iter module.
Import with import std/vector.
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(): 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(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(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(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(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(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(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(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.vector_next
Section titled “Vector.vector_next”fn vector_next(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.
Vector.next
Section titled “Vector.next”impl Iterable
fn next(vector: Vector<'T>): Maybe<('T, Vector<'T>)>
impl Iterable.next
Vector.known_count
Section titled “Vector.known_count”impl Iterable
fn known_count(vector: Vector<'T>): Maybe<Int>
impl Iterable.known_count
Vector.add
Section titled “Vector.add”impl Add
fn add(lhs: Vector<'T>, rhs: Vector<'T>): Vector<'T>
impl Add.add
Vector.to_string
Section titled “Vector.to_string”impl Display
fn to_string(vector: Vector<'T>): String
impl Display.to_string
Vector.inspect
Section titled “Vector.inspect”impl Debug
fn inspect(vector: Vector<'T>): String
impl Debug.inspect
Vector.equal?
Section titled “Vector.equal?”impl Equatable
fn equal?(a: Vector<'T>, b: Vector<'T>): Bool
impl Equatable.equal?
Vector.hash
Section titled “Vector.hash”impl Hashable
fn hash(vector: Vector<'T>): Int
impl Hashable.hash
Vector.compare
Section titled “Vector.compare”impl Comparable
fn compare(a: Vector<'T>, b: Vector<'T>): Ordering
impl Comparable.compare