list
Types
List
Functions
from_array
fn(arr: Array<'t>) -> List<'t>
Converts an array to a list, preserving element order.
import core/option { Some }
let l = list.from_array([1, 2, 3]);
let (a, rest) = list.pop_front(l);
let (b, rest) = list.pop_front(rest);
let (c, _) = list.pop_front(rest);
test.assert_eq(a, Some(1));
test.assert_eq(b, Some(2));
test.assert_eq(c, Some(3));is_empty
fn(list: List<'t>) -> Bool
length
fn(list: List<'a>) -> Int
Returns the number of elements in the list.
let l = list.new()
|> list.push_front(1)
|> list.push_front(2)
|> list.push_front(3);
test.assert_eq(list.length(l), 3);map
fn(list: List<'a>, map_fn: fn ('a) -> 'b / 't88) -> List<'b> / { 't88, 't88, ..'t111 }
Transforms each element in the list by applying a function.
import core/option { Some }
let l = list.new()
|> list.push_front(1)
|> list.push_front(2)
|> list.push_front(3);
let doubled = list.map(l, fn(x) { x * 2 });
let (a, rest) = list.pop_front(doubled);
let (b, rest) = list.pop_front(rest);
let (c, _) = list.pop_front(rest);
test.assert_eq(a, Some(6));
test.assert_eq(b, Some(4));
test.assert_eq(c, Some(2));new
∀ 't . fn () -> List<'t>
Creates a new empty list.
test.assert(list.is_empty(list.new()));peek
fn(list: List<'t>) -> Option<'t>
pop_front
fn(list: List<'t>) -> (Option<'t>, List<'t>)
push_front
fn(list: List<'t>, value: 't) -> List<'t>
Adds an element to the front of the list.
import core/option { Some }
let (x, xs) = list.new()
|> list.push_front(1)
|> list.pop_front();
test.assert(list.is_empty(xs));
test.assert_eq(x, Some(1));reduce
fn(list: List<'a>, start: 'b, reducer: fn ('b, 'a) -> 'b / { 't149, ..'t149 }) -> 'b / { 't149, ..'t149 }
Reduces a list to a single value by applying a function to each element.
let l = list.new()
|> list.push_front(1)
|> list.push_front(2)
|> list.push_front(3);
test.assert_eq(list.reduce(l, 0, fn(acc, x) { acc + x }), 6);replace_front_with
fn(list: List<'t>, update_with: fn ('t) -> 't / 'r) -> List<'t> / 'r
Updates the item at the front of the list by applying the function to it. If the list is empty, returns an empty list.
reverse
fn(list: List<'a>) -> List<'a>
Reverses the order of elements in the list.
import core/option { Some }
let l = list.new()
|> list.push_front(1)
|> list.push_front(2)
|> list.push_front(3);
let rev = list.reverse(l);
let (a, rest) = list.pop_front(rev);
let (b, rest) = list.pop_front(rest);
let (c, _) = list.pop_front(rest);
test.assert_eq(a, Some(1));
test.assert_eq(b, Some(2));
test.assert_eq(c, Some(3));to_array
fn(list: List<'a>) -> Array<'a>
Converts a list to an array.
let l = list.new()
|> list.push_front(1)
|> list.push_front(2)
|> list.push_front(3);
test.assert_eq(list.to_array(l), [3, 2, 1]);