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)
classShow#
pub class Show A {
    def show : A -> String
}
instance_#
pub instance [Show A] Show (List A) {
    def show (xs : List A) : String :=
        list_show (fn a => Show.show a) xs
}
instance_#
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
        }
}
instance_#
pub instance {A : Type} Append (List A) {
    def append (a b : List A) : List A := List.append a b
}
defList.filter#
pub def List.filter {A : Type} (pred : A -> Bool) (xs : List A) : List A
defList.any#
pub def List.any {A : Type} (pred : A -> Bool) (xs : List A) : Bool
defList.all#
pub def List.all {A : Type} (pred : A -> Bool) (xs : List A) : Bool
defList.filter_map#
pub def List.filter_map {A : Type} {B : Type} (f : A -> Option B) (xs : List A) : List B

Map + keep only the somes, in one pass. Replaces Toml.filter_some and ~11 collect/filter defs in lang/scope.mo.

defList.contains_by#
pub def List.contains_by {A : Type} (eq : A -> A -> Bool) (target : A) (xs : List A) : Bool

Membership 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.

defList.intercalate#
pub def List.intercalate (sep : String) (xs : List String) : String

Join string pieces with a separator between them. Replaces 28 hand-rolled *_rest joiner pairs across 9 lang/ files.

defList.dedup_by#
pub def List.dedup_by {A : Type} (eq : A -> A -> Bool) (xs : List A) : List A

Drop later duplicates, keeping the first occurrence (left-biased). Replaces union_ids/dedup_idents/dedup_funcs_by_name.

defList.sort_by#
pub def List.sort_by {A : Type} (lt : A -> A -> Bool) (xs : List A) : List A

Stable 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.