std/lists
Operations on the persistent linked List<T> type. Container-specific ops
(concat, head, tail) live here; generic iteration — including
Iter.count (O(1) via the known_count protocol) — goes through
Iter: Iter.map(xs, f) |> Iter.to_list() for the strict,
List-returning form.
Import with import std/lists.
Exports
Section titled “Exports”Types
type List
Section titled “type List”type List<T>
Ordered, immutable sequence of elements.
List.next_item
Section titled “List.next_item”fn next_item<T>(list: List<T>): Maybe<(T, List<T>)>
type function on List
Returns the next element and remaining list, or None if empty. Drives the
Iter implementation for List.
Interactive Tests
assert List.next_item([1, 2, 3]) == Some((1, [2, 3]))
assert List.next_item([]) == None
List.concat
Section titled “List.concat”fn concat<T>(a: List<T>, b: List<T>): List<T>
type function on List
Concatenates two lists, yielding all elements of a followed by all of b.
Interactive Tests
assert List.concat([1, 2], [3, 4]) == [1, 2, 3, 4]
List.head
Section titled “List.head”fn head<T>(list: List<T>): Maybe<T>
type function on List
Returns the first element, or None if the list is empty. The cons-list idiom,
paired with tail; the stream-vocabulary equivalent is the free function
Iter.first.
Interactive Tests
assert List.head([1, 2, 3]) == Some(1)
assert List.head([]) == None
List.tail
Section titled “List.tail”fn tail<T>(list: List<T>): Maybe<List<T>>
type function on List
Returns all elements except the first, or None if the list is empty.
Interactive Tests
assert List.tail([1, 2, 3]) == Some([2, 3])
assert List.tail([]) == None
List.each_while
Section titled “List.each_while”fn each_while(list: List<T>, yield: (T) -> Bool): Bool
impl Iter.each_while
List.known_count
Section titled “List.known_count”fn known_count(list: List<T>): Maybe<Int>
impl Iter.known_count
List.add
Section titled “List.add”fn add(lhs: List<T>, rhs: List<T>): List<T>
impl Add.add
List.to_string
Section titled “List.to_string”fn to_string(value: List<T>): String
impl Display.to_string
List.inspect
Section titled “List.inspect”fn inspect(value: List<T>): String
impl Debug.inspect
List.equal?
Section titled “List.equal?”fn equal?(a: List<T>, b: List<T>): Bool
impl Equatable.equal?
List.hash
Section titled “List.hash”fn hash(list: List<T>): Int
impl Hashable.hash
List.compare
Section titled “List.compare”fn compare(a: List<T>, b: List<T>): Ordering
impl Comparable.compare