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.
Exports
Section titled “Exports”Types
type Vector
Section titled “type Vector”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.
Vector.length
Section titled “Vector.length”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
Vector.get
Section titled “Vector.get”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"
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. O(log32 n).
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
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"]
Vector.concat
Section titled “Vector.concat”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]
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.
assert Vector.next_item([1, 2]) == Some((1, [2]))
assert Vector.next_item([]) == None
Vector.each_while
Section titled “Vector.each_while”impl Iter for Vector<T>
fn each_while(vector: Vector<T>, yield: (T) -> Bool) -> Bool
impl Iter.each_while
Vector.known_count
Section titled “Vector.known_count”impl Iter for Vector<T>
fn known_count(vector: Vector<T>) -> Maybe<Int>
impl Iter.known_count
Vector.contains
Section titled “Vector.contains”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)
Vector.add
Section titled “Vector.add”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"]
Vector.empty_sum
Section titled “Vector.empty_sum”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]
Vector.to_string
Section titled “Vector.to_string”impl Display for Vector<T>
fn to_string(vector: Vector<T>) -> String
impl Display.to_string
Vector.inspect
Section titled “Vector.inspect”impl Debug for Vector<T>
fn inspect(vector: Vector<T>) -> String
impl Debug.inspect
Vector.equal
Section titled “Vector.equal”impl Equatable for Vector<T>
fn equal(a: Vector<T>, b: Vector<T>) -> Bool
impl Equatable.equal
Vector.hash
Section titled “Vector.hash”impl Hashable for Vector<T>
fn hash(vector: Vector<T>) -> Int
impl Hashable.hash
Vector.compare
Section titled “Vector.compare”impl Comparable for Vector<T>
fn compare(a: Vector<T>, b: Vector<T>) -> Ordering
impl Comparable.compare
type NonEmptyVector
Section titled “type NonEmptyVector”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
NonEmptyVector.of
Section titled “NonEmptyVector.of”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]
NonEmptyVector.first
Section titled “NonEmptyVector.first”fn first<T>(vector: NonEmptyVector<T>) -> T
type function on NonEmptyVector
The first element.
assert NonEmptyVector.first([4, 5]) == 4
NonEmptyVector.last
Section titled “NonEmptyVector.last”fn last<T>(vector: NonEmptyVector<T>) -> T
type function on NonEmptyVector
The last element.
assert NonEmptyVector.last([4, 5]) == 5
NonEmptyVector.length
Section titled “NonEmptyVector.length”fn length<T>(vector: NonEmptyVector<T>) -> PositiveInt
type function on NonEmptyVector
The number of elements, at least one.
assert NonEmptyVector.length(["a", "b"]) == 2
NonEmptyVector.max
Section titled “NonEmptyVector.max”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
NonEmptyVector.min
Section titled “NonEmptyVector.min”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
NonEmptyVector.mean
Section titled “NonEmptyVector.mean”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
NonEmptyVector.reduce
Section titled “NonEmptyVector.reduce”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
NonEmptyVector.map
Section titled “NonEmptyVector.map”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]
NonEmptyVector.with_index
Section titled “NonEmptyVector.with_index”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")]
NonEmptyVector.push
Section titled “NonEmptyVector.push”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]
NonEmptyVector.concat
Section titled “NonEmptyVector.concat”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]
NonEmptyVector.try_from
Section titled “NonEmptyVector.try_from”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.