std.list
The list manipulation module. Pay particular attention to move semantics: there are two kinds of functions—those that borrow the source list read-only, and those that consume (move) the source list. For the automatic borrowing rules of & parameters, see RFC-009 §2.8: when the argument is still used after the call, the compiler automatically creates a read-only token.
use std.listSemantic Categories
Parameters with & in the signature are read-only borrows, and the source value remains usable after the call; parameters without & are passed by value, so the source value is moved after the call and using it again reports E2014.
| Category | Functions | Behavior |
|---|---|---|
| Consumes the source list | push append prepend set pop remove_at iter | The source list is moved and cannot be used afterward |
| Read-only borrow | len is_empty get first last slice reverse concat contains find_index map filter reduce | The source list can be reused; item in contains/find_index is also borrowed (&A) |
| Iterator protocol | has_next (&Iter(T)) next (&mut Iter(T)) | Both borrow the iterator and do not consume it |
use std.assert
use std.list
main: () -> Void = {
nums = [1, 2, 3]
// Read-only borrow: nums can be reused
assert(list.len(nums) == 3)
assert(list.len(nums) == 3)
assert(list.contains(nums, 2))
// Consumes: base cannot be used after this
base = [1, 2]
extended = list.push(base, 3)
assert(list.len(extended) == 3)
}Function Overview
| Function | Signature |
|---|---|
push | (A: Type) -> (list: Vec(A), item: A) -> Vec(A) |
pop | (A: Type) -> (list: Vec(A)) -> Vec(A) |
append | (A: Type) -> (list: Vec(A), item: A) -> Vec(A) |
prepend | (A: Type) -> (list: Vec(A), item: A) -> Vec(A) |
remove_at | (A: Type) -> (list: Vec(A), index: Int) -> Vec(A) |
reverse | (A: Type) -> (list: &Vec(A)) -> Vec(A) |
concat | (A: Type) -> (a: &Vec(A), b: &Vec(A)) -> Vec(A) |
map | (T: Type, R: Type) -> (list: &Vec(T), f: (item: T) -> R) -> Vec(R) |
filter | (T: Type) -> (list: &Vec(T), keep: (item: T) -> Bool) -> Vec(T) |
reduce | (T: Type, Acc: Type) -> (list: &Vec(T), f: (acc: Acc, item: T) -> Acc, init: Acc) -> Acc |
len | (A: Type) -> (list: &Vec(A)) -> Int |
is_empty | (A: Type) -> (list: &Vec(A)) -> Bool |
get | (A: Type) -> (list: &Vec(A), index: Int) -> A |
set | (A: Type) -> (list: Vec(A), index: Int, value: A) -> Vec(A) |
first | (A: Type) -> (list: &Vec(A)) -> A |
last | (A: Type) -> (list: &Vec(A)) -> A |
slice | (A: Type) -> (list: &Vec(A), start: Int, end: Int) -> Vec(A) |
contains | (A: Type) -> (list: &Vec(A), item: &A) -> Bool |
find_index | (A: Type) -> (list: &Vec(A), item: &A) -> Int |
Iter | (T: Type) -> Type |
iter | (T: Type) -> (list: Vec(T)) -> Iter(T) |
next | (T: Type) -> (it: &mut Iter(T)) -> T |
has_next | (T: Type) -> (it: &Iter(T)) -> Bool |
empty | (T: Type) -> Vec(T) |
of | (T: Type) -> (data: Vec(T)) -> Vec(T) |
Functions
push
push: (A: Type) -> (list: Vec(A), item: A) -> Vec(A)Returns a new list with item appended to the end of list. list is passed by value and is therefore moved after the call; it cannot be used again.
use std.assert
use std.list
main: () -> Void = {
base = [1, 2]
extended = list.push(base, 3)
assert(list.len(extended) == 3)
}append
append: (A: Type) -> (list: Vec(A), item: A) -> Vec(A)An alias for push; the behavior is exactly the same.
use std.assert
use std.list
main: () -> Void = {
extended = list.append([1, 2], 3)
assert(list.len(extended) == 3)
}prepend
prepend: (A: Type) -> (list: Vec(A), item: A) -> Vec(A)Returns a new list with item inserted at the beginning of list. list is passed by value and is therefore moved after the call.
use std.assert
use std.list
main: () -> Void = {
l = list.prepend([2, 3], 1)
assert(list.first(l) == 1)
}pop
pop: (A: Type) -> (list: Vec(A)) -> Vec(A)Removes the last element and returns the shortened list (value semantics). The source list is consumed; it is no longer the exceptional native form in which the signature includes & while modifying the source value in place.
Returns: a new list with the last element removed; if the list is empty, it is returned unchanged. To read the removed element, call last before the call.
use std.assert
use std.list
main: () -> Void = {
l = [1, 2, 3]
rest = list.pop(l) // l is consumed; rest is the shortened new list
assert(list.len(rest) == 2)
assert(list.last(rest) == 2) // the last element, 3, has been removed
// To read the removed element, call last before pop
l2 = [1, 2, 3]
removed = list.last(l2)
assert(removed == 3)
empty = list.empty(Int)
assert(list.is_empty(list.pop(empty)))
}remove_at
remove_at: (A: Type) -> (list: Vec(A), index: Int) -> Vec(A)Removes the element at index index and returns a new, shortened list (value semantics). The source list is consumed.
index— element index
Returns: a new list with that element removed. Error: if the index is negative, E6003 is thrown (out-of-bounds access to list[i + 1] at src/std/list.yx:156-158).
Out-of-bounds access does not report an error: when the index is ≥ the length, the transfer loop is never entered; only
lengthis decremented by 1, silently truncating the list—list.remove_at([1, 2, 3], 5)returns a two-element list and does not report an error. Check the bounds yourself before calling.
use std.assert
use std.list
main: () -> Void = {
l = [10, 20, 30]
got = list.remove_at(l, 1)
assert(list.len(got) == 2)
assert(list.get(got, 0) == 10)
assert(list.get(got, 1) == 30)
}set
set: (A: Type) -> (list: Vec(A), index: Int, value: A) -> Vec(A)Returns a new list with the element at index index replaced by value. list is passed by value and is therefore moved after the call.
index— indexvalue— new value
Error: if the index is negative or ≥ the length, E6003 is thrown (out-of-bounds writes are no longer silently discarded).
use std.assert
use std.list
main: () -> Void = {
l = list.set([1, 2, 3], 1, 99)
assert(list.get(l, 1) == 99)
}get
get: (A: Type) -> (list: &Vec(A), index: Int) -> AReads the element at index index (read-only borrow; list can be reused).
index— index
Returns: the element value.
Error: if the index is negative or ≥ the length, E6003 is thrown (src/std/list.yx:78 accesses list[index] directly; the bounds check in [] handles the out-of-bounds access, so it does not return Void).
use std.assert
use std.list
main: () -> Void = {
nums = [1, 2, 3, 4]
assert(list.get(nums, 1) == 2)
}first
first: (A: Type) -> (list: &Vec(A)) -> AReturns the first element.
Error: an empty list raises E6003—the fallback branch at src/std/list.yx:82-89 reads zero[0], making index 0 out of bounds for an empty Vec. Check is_empty first.
use std.assert
use std.list
main: () -> Void = {
assert(list.first([1, 2, 3]) == 1)
}last
last: (A: Type) -> (list: &Vec(A)) -> AReturns the last element.
Error: an empty list raises E6003 (same as first, src/std/list.yx:92-99).
use std.assert
use std.list
main: () -> Void = {
assert(list.last([1, 2, 3]) == 3)
}slice
slice: (A: Type) -> (list: &Vec(A), start: Int, end: Int) -> Vec(A)Takes a sublist of the [start, end) range.
start— starting indexend— ending index (exclusive)
Returns: a new list. If start >= end, returns an empty list.
Error: bounds are not clamped—src/std/list.yx:196-205 reads list[i] one element at a time, and an out-of-bounds index (including a negative start or end) immediately raises E6003. list.slice([1, 2, 3], 1, 99) raises an error instead of returning a two-element list.
use std.assert
use std.list
main: () -> Void = {
sub = list.slice([1, 2, 3, 4], 1, 3)
assert(list.len(sub) == 2)
assert(list.first(sub) == 2)
}reverse
reverse: (A: Type) -> (list: &Vec(A)) -> Vec(A)Returns a new list with the elements in reverse order; the source list is unchanged.
use std.assert
use std.list
main: () -> Void = {
rev = list.reverse([1, 2, 3])
assert(list.first(rev) == 3)
}concat
concat: (A: Type) -> (a: &Vec(A), b: &Vec(A)) -> Vec(A)Concatenates two lists and returns a new list. Both source lists remain unchanged.
Type checking is performed at compile time: concat is statically generic (src/std/list.yx:179); if the second argument is not a list, a type error is reported. There is no runtime E6007 path.
use std.assert
use std.list
main: () -> Void = {
joined = list.concat([1, 2], [3, 4])
assert(list.len(joined) == 4)
}len
len: (A: Type) -> (list: &Vec(A)) -> IntThe number of elements. This is a read-only borrow, so list can be reused.
len is statically generic (src/std/list.yx:67); a non-list argument is a compile-time type error, not a runtime exception.
use std.assert
use std.list
main: () -> Void = {
nums = [1, 2, 3]
assert(list.len(nums) == 3)
assert(list.len(nums) == 3) // can be reused
}is_empty
is_empty: (A: Type) -> (list: &Vec(A)) -> BoolWhether the list is empty.
Like len, this is statically generic (src/std/list.yx:72); a non-list argument is a compile-time type error.
use std.assert
use std.list
main: () -> Void = {
assert(list.is_empty([]))
assert(!list.is_empty([1]))
}contains
contains: (A: Type) -> (list: &Vec(A), item: &A) -> BoolWhether item is in the list (compared by value equality; the element type must support ==—primitive types support it natively, while record types obtain it through automatic derivation or an explicit Equal instantiation from RFC-011b). item is passed as a read-only borrow (the call site automatically creates an &A token), so the argument remains usable after the call.
Returns: true if present; false if absent (linear scan; no errors are thrown).
use std.assert
use std.list
main: () -> Void = {
nums = [1, 2, 3, 4]
assert(list.contains(nums, 3))
assert(!list.contains(nums, 99))
}find_index
find_index: (A: Type) -> (list: &Vec(A), item: &A) -> IntThe index of the first occurrence of item. item is passed as a read-only borrow (the call site automatically creates an &A token), so the argument remains usable after the call.
Returns: the index if found; -1 if not found.
use std.assert
use std.list
main: () -> Void = {
assert(list.find_index([1, 2, 3, 4], 3) == 2)
assert(list.find_index([1, 2], 99) == -1)
}map
map: (T: Type, R: Type) -> (list: &Vec(T), f: (item: T) -> R) -> Vec(R)Calls fn for each element and returns a new list composed of the results. Passing a function value uses a curried form: list.map(nums, x => x * 2). The source list is unchanged.
map is statically generic (src/std/list.yx:210); if the second argument is not a function, a compile-time type error is reported.
use std.assert
use std.list
main: () -> Void = {
doubled = list.map([1, 2, 3], x => x * 2)
assert(list.get(doubled, 0) == 2)
}filter
filter: (T: Type) -> (list: &Vec(T), keep: (item: T) -> Bool) -> Vec(T)Keeps the elements for which fn is true. The source list is unchanged.
filter is statically generic (src/std/list.yx:222); if the second argument is not a function, a compile-time type error is reported.
use std.assert
use std.list
main: () -> Void = {
evens = list.filter([1, 2, 3, 4], x => x % 2 == 0)
assert(list.len(evens) == 2)
}reduce
reduce: (T: Type, Acc: Type) -> (list: &Vec(T), f: (acc: Acc, item: T) -> Acc, init: Acc) -> AccFolds from left to right: starting with init, calls fn(acc, item) in sequence.
fn— reduction function(accumulator, element) -> new accumulatorinit— initial accumulator
Returns: the final accumulator. If the list is empty, returns init.
reduce is statically generic (src/std/list.yx:241); if the second argument is not a function, a compile-time type error is reported. Note that f receives the accumulator and element by value—each iteration passes the current accumulator to f and uses the return value as the accumulator for the next iteration.
use std.assert
use std.list
main: () -> Void = {
total = list.reduce([1, 2, 3, 4], (acc, x) => acc + x, 0)
assert(total == 10)
}iter
iter: (T: Type) -> (list: Vec(T)) -> Iter(T)Creates an iterator. The iterator is the record type Iter(T) exported by this module and contains two named fields—a buffer and a cursor (src/std/list.yx:259-262):
Iter: (T: Type) -> Type = {
buf: Vec(T),
pos: Int,
}The source list is passed by value and is therefore moved after the call.
Returns: an Iter(T) value for use with next / has_next.
use std.assert
use std.list
main: () -> Void = {
it = list.iter([1, 2, 3])
assert(list.has_next(it))
}next
next: (T: Type) -> (it: &mut Iter(T)) -> TRemoves the current element and advances the internal cursor pos in place by one.
Returns: the current element.
Borrowing semantics:
nexttakes&mut Iter(T), whilehas_nexttakes&Iter(T); neither moves the iterator, so the same iterator can be used to retrieve elements consecutively. Before calling, checkhas_next: attempting to retrieve after iteration has ended is caught by theVecbounds check and reportsE6003; it does not returnVoid(src/std/list.yx:277-285).This is the opposite of
std.range.next—range.has_nexthas no&in its signature and consumes the iterator by value.
use std.assert
use std.list
main: () -> Void = {
it = list.iter([7, 8])
// Retrieve repeatedly from the same iterator—next borrows it and does not consume it
assert(list.next(it) == 7)
assert(list.next(it) == 8)
assert(!list.has_next(it))
}has_next
has_next: (T: Type) -> (it: &Iter(T)) -> BoolWhether any unconsumed elements remain. This is a read-only borrow of &Iter(T) and does not consume the iterator.
use std.assert
use std.list
main: () -> Void = {
it = list.iter([1])
assert(list.has_next(it))
assert(list.next(it) == 1)
assert(!list.has_next(it))
}for ... in Traversal
Lists can be traversed directly with for ... in, without manually calling next:
use std.assert
main: () -> Void = {
mut sum = 0
for x in [1, 2, 3] {
sum = sum + x
}
assert(sum == 6)
}Related
std.range— range iteration and lazy adaptersstd.assert— assertion utility used in the examples
