std::list
std/src/list.mo
Standard library: List operations and instances not covered in init/
Provides:
- Show class and instances (String, I64, Bool)
- Show (List A) instance — element-wise, uses runtime type dispatch
- Append (List A) instance
- BEq (List A) instance — full element-wise equality
- List.filter, List.sum
- list_show helper (explicit show function, for concrete call sites)
pub class Show A {
def show : A -> String
}pub instance [Show A] Show (List A) {
def show (xs : List A) : String :=
list_show (fn a => Show.show a) xs
}pub instance [BEq A] BEq (List A) {
def beq (xs ys : List A) : Bool :=
match xs {
empty => match ys {
empty => true,
cons _ _ => false,
_ => false
},
cons x x_tail => match ys {
empty => false,
cons y y_tail =>
(x == y) && (BEq.beq x_tail y_tail),
_ => false
},
_ => false
}
}pub instance {A : Type} Append (List A) {
def append (a b : List A) : List A := List.append a b
}pub def List.filter {A : Type} (pred : A -> Bool) (xs : List A) : List Apub def List.any {A : Type} (pred : A -> Bool) (xs : List A) : Boolpub def List.all {A : Type} (pred : A -> Bool) (xs : List A) : Boolpub def List.filter_map {A : Type} {B : Type} (f : A -> Option B) (xs : List A) : List BMap + keep only the somes, in one pass.
Replaces Toml.filter_some and ~11 collect/filter defs in lang/scope.mo.
pub def List.contains_by {A : Type} (eq : A -> A -> Bool) (target : A) (xs : List A) : BoolMembership using an explicit equality function.
Replaces id_member/ident_in_list/list_contains/str_list_contains/...
pub because that "Replaces ..." list is the whole point of it and it
could not act on anything outside std while package-private:
build/src/closure.mo wants exactly this for its visited set, and
hand-rolling a fourth copy is the duplication this def exists to end.
pub def List.intercalate (sep : String) (xs : List String) : StringJoin string pieces with a separator between them.
Replaces 28 hand-rolled *_rest joiner pairs across 9 lang/ files.
pub def List.dedup_by {A : Type} (eq : A -> A -> Bool) (xs : List A) : List ADrop later duplicates, keeping the first occurrence (left-biased).
Replaces union_ids/dedup_idents/dedup_funcs_by_name.
pub def List.sort_by {A : Type} (lt : A -> A -> Bool) (xs : List A) : List AStable insertion sort with an explicit lt comparator. Stable: equal
elements keep their relative order. The comparator is explicit for the
same reason contains_by/dedup_by take one -- class-based dispatch is
runtime-only and unreliable inside recursive self-hosted code.