Skip to reference

std.list

wolf-std d58f9e1 · toolchain pin verified against wolf 0.2.41 (0261597). Source signatures and documentation; execution support is stated in each module.

All modules · Plain text · Stability tier: core

Module

std.list — the documented API surface of the language's List[T]: the growable sequence every other module hands back. Stability tier: core.

What is the language's and what is std's (sc02's structural finding, F-0011): List[T] is a language-blessed core type — both implementations provide it natively, the spec owns its semantics, and the three operations below stay the language's own:

- construction: List[T](); - indexing: xs[i] reads, and an out-of-range index traps bounds in every profile (X3 posture) — every function here that names an index inherits exactly that trap; - iteration: for x in xs.

Everything else — this file — is std's. Functions take the container as their first parameter with an honest X1 mode: mut where mutation IS the operation, the bare read default everywhere else.

Two operations joined the language's list at the sc44 pins and they are worth naming because a learner reaches for them first: SLICING (xs[..n], xs[1..], [mem.list.slice]) runs on all three lanes now, and it is the language's, not std's. There is no list.prefix/list.suffix and there will not be one — §8 forbids a std name that shadows a working builtin — and there cannot be a list.take either, because take is a reserved word (E0008 on both machines: the ownership verb owns it).

THE METHOD SPELLINGS ARE NOT STD'S TO GIVE (wolf-std#25). A learner coming from chapter 5 writes (mut l).truncate(2) or l.remove(i) and gets "wolf cannot compile this yet — methods on generic std data" from the compiler and "List has no method truncate in this machine's std subset" from lupin. Both refusals are about the LANGUAGE's method surface on the builtin List, and std cannot extend it: this module is free functions over a container, by §8's rule, and list.truncate(mut l, 2) is the spelling it offers. All three of insert, remove and truncate are here, with bodies, with fenced doc examples that execute, and with three-lane ledger rows (list/insert_remove.lu, list/clear_truncate_fill.lu) — so the gap the issue found is a gap in what a learner is TOLD, not in what std ships. None of the three is written over the slice; since sc72 none rebuilds either (the paragraph below).

The mutators work in place (sc72, wolf-std#92). set, insert, remove, swap_remove, swap, clear, truncate, reverse, fill, pop and sort_by change the caller's list through mut by the language's index store (xs[i] = copy v), its push and pop, and its clear — where through sc71 each rebuilt its result into a fresh List[T]() and assigned it back. Two things follow. A call keeps nothing per call where it used to allocate a whole list (an element type that reaches no storage allocates nothing at all, and a str or a List element pays one copy per slot written, not one per element; tests/list/in_place_allocates_nothing.lu). And the mut parameter is AMBIENT-CLEAN under [mem.region.outer-write] (wolf 0.2.35; wolf conform-run --dump=mut-clean says clean for each): a program may call them inside a per-turn region block on a list declared outside it, which the rebuilds made E1010 by name. The signatures did not move. sort_by still merges over scratch lists in the ambient region and writes the order back. push and extend store the caller's value as given and stay unclean: a copy there would deep-copy every pushed str, and inside a block a caller writes (mut xs).push(copy v) itself. The helpers that RETURN a list (concat, repeat, map, filter, sorted_by, …) build it fresh, which is what they are for.

Regions (D10): every function that builds a list allocates it in the ambient region, a mutator's stores land in the list's own, and no signature asks for one. The .in(r) targets are s37's plumbing, not this module's. s76 made that sentence true of the machine and not only of the API (the sc12 pin): a List allocates in the ambient region AT THE SITE, so a list built inside region r { } is freed with it and growth stays in the region it was born in. Nothing here changed to earn that, which is the point — a region-silent algorithm inherits its operands' homes, and now it does so in fact rather than in intention.

HISTORY, kept because two campaigns of bodies were shaped by it: - The single-read rule is DEAD (s72's D40, [mem.iter.excl], closed at the sc11 pin). wolfc's mem tier used to treat every read of a container place as a MOVE, so two read SITES over one binding in one body was E1001 (xs.len twice; xs.len then xs[i]; for x in xs then anything), and that is why every body here reads through len and the private at — one call per site — instead of projecting twice. Iteration takes a read claim over the loop's whole extent now, for no longer moves its iterable, and a write during the loop is E1013 statically and trap(exclusivity) dynamically (F-0005 and F-0014, both closed). The bodies keep their shape because they are correct and rewriting them would be churn; new code in this module does not need the ceremony. - The absence-spelling interim is retired: the tag is lowercase everywhere as of sc06 (F-0003 dead), and this header carried a half-renamed sentence about it — "none, not the convention's none" — for six sprints afterwards. A rename that touches 148 sites should have a reader on the prose too.

INTERIM SPELLINGS (each filed, flipped by the pin bump that fixes it): - The [T: Eq] family (contains, index_of, last_index_of, dedup, starts_with, ends_with) is NOT in this file: importing std.cmp for the bound would make every importer of std.list unsupported under wolfc's checked tier (its trait/enum items are not executable there — F-0012), on top of trait dispatch running nowhere (F-0002/F-0004). The six sit in std/x/list_eq/ with their bodies, and the stub comment below is their home here.

Types and traits

No public declarations in this section.

Functions and methods

push

fn · Source line 145

pub fn push[T](mut xs: List[T], v: T)

Appends v to the end of xs, growing it. Amortized constant time (the buffer doubles; no capacity contract beyond that is promised). Total: no error row, no traps beyond allocation.

var xs = List[int]()
list.push(mut xs, 4)
list.push(mut xs, 5)
list.len(xs) == 2

pop

fn · Source line 159

pub fn pop[T](mut xs: List[T]) -> T ! {none}

Removes and returns the last element; none when xs is empty — the absence row, consumed by else or ?. No traps: emptiness is a recoverable outcome, not a contract violation (§2).

var xs = List[int]()
list.push(mut xs, 7)
(list.pop(mut xs) else 0 - 1) == 7
(list.pop(mut xs) else 0 - 1) == 0 - 1

len

fn · Source line 176

pub fn len[T](xs: List[T]) -> int

The number of elements in xs. Total: no error row, no traps.

var xs = List[int]()
list.push(mut xs, 1)
list.len(xs) == 1

is_empty

fn · Source line 189

pub fn is_empty[T](xs: List[T]) -> bool

Whether xs holds no elements — len(xs) == 0, spelled as the predicate callers reach for (§1). Total: no error row, no traps.

var xs = List[int]()
list.is_empty(xs) == true
list.push(mut xs, 1)
list.is_empty(xs) == false

get

fn · Source line 205

pub fn get[T](xs: List[T], i: int) -> T ! {none}

The element at i, checked: none when i is negative or at least len(xs) — the one checked-access spelling (§1: no get_ prefix, no sentinel). No traps; the unchecked spelling is the language's xs[i], which traps bounds.

var xs = List[int]()
list.push(mut xs, 9)
(list.get(xs, 0) else 0 - 1) == 9
(list.get(xs, 4) else 0 - 1) == 0 - 1
(list.get(xs, 0 - 1) else 0 - 1) == 0 - 1

set

fn · Source line 227

pub fn set[T](mut xs: List[T], i: int, v: T)

Replaces the element at i with v, in place. Traps bounds when i is out of range — the same contract as xs[i], and a caller violation rather than a row (§2's trap rule).

var xs = List[int]()
list.push(mut xs, 1)
list.push(mut xs, 2)
list.set(mut xs, 0, 8)
(list.get(xs, 0) else 0) == 8
(list.get(xs, 1) else 0) == 2

first

fn · Source line 249

pub fn first[T](xs: List[T]) -> T ! {none}

The first element; none when xs is empty. No traps.

var xs = List[int]()
list.push(mut xs, 3)
list.push(mut xs, 4)
(list.first(xs) else 0) == 3

last

fn · Source line 264

pub fn last[T](xs: List[T]) -> T ! {none}

The last element; none when xs is empty. No traps.

var xs = List[int]()
list.push(mut xs, 3)
list.push(mut xs, 4)
(list.last(xs) else 0) == 4

insert

fn · Source line 284

pub fn insert[T](mut xs: List[T], i: int, v: T)

Inserts v at i, shifting the elements from i onward one place to the right; i == len(xs) appends. Traps bounds when i is negative or greater than len(xs).

var xs = List[int]()
list.push(mut xs, 1)
list.push(mut xs, 3)
list.insert(mut xs, 1, 2)
(list.get(xs, 1) else 0) == 2
list.len(xs) == 3

remove

fn · Source line 317

pub fn remove[T](mut xs: List[T], i: int) -> T

Removes the element at i, shifting the rest left, and returns it. Traps bounds when i is out of range — the caller asked for an element that is not there and could have checked len first.

var xs = List[int]()
list.push(mut xs, 1)
list.push(mut xs, 2)
list.remove(mut xs, 0) == 1
list.len(xs) == 1

swap_remove

fn · Source line 345

pub fn swap_remove[T](mut xs: List[T], i: int) -> T

Removes the element at i by moving the LAST element into its slot, and returns the removed element. Constant time, and the order of the remaining elements changes — that is the trade this name announces. Traps bounds when i is out of range.

var xs = List[int]()
list.push(mut xs, 1)
list.push(mut xs, 2)
list.push(mut xs, 3)
list.swap_remove(mut xs, 0) == 1
(list.get(xs, 0) else 0) == 3
list.len(xs) == 2

swap

fn · Source line 366

pub fn swap[T](mut xs: List[T], i: int, j: int)

Exchanges the elements at i and j, in place; i == j is a no-op. Traps bounds when either index is out of range.

var xs = List[int]()
list.push(mut xs, 1)
list.push(mut xs, 2)
list.swap(mut xs, 0, 1)
(list.get(xs, 0) else 0) == 2
(list.get(xs, 1) else 0) == 1

clear

fn · Source line 382

pub fn clear[T](mut xs: List[T])

Empties xs, keeping it usable. Total: no error row, no traps. D10 note: the elements' storage belongs to the region, so clearing drops no elements one by one for POD element types.

var xs = List[int]()
list.push(mut xs, 1)
list.clear(mut xs)
list.is_empty(xs) == true

truncate

fn · Source line 398

pub fn truncate[T](mut xs: List[T], n: int)

Shortens xs to at most n elements, dropping the tail; a n at or above len(xs) leaves it alone and a negative n empties it. Total: no error row, no traps (truncating is not an access).

var xs = List[int]()
list.push(mut xs, 1)
list.push(mut xs, 2)
list.push(mut xs, 3)
list.truncate(mut xs, 2)
list.len(xs) == 2

reverse

fn · Source line 449

pub fn reverse[T](mut xs: List[T])

Reverses xs in place. Total: no error row, no traps.

var xs = List[int]()
list.push(mut xs, 1)
list.push(mut xs, 2)
list.push(mut xs, 3)
list.reverse(mut xs)
(list.first(xs) else 0) == 3
(list.last(xs) else 0) == 1

extend

fn · Source line 472

pub fn extend[T](mut xs: List[T], other: List[T])

Appends every element of other to xs, in order. other is read, not consumed — the caller keeps it. Total: no error row, no traps beyond allocation.

var xs = List[int]()
var ys = List[int]()
list.push(mut xs, 1)
list.push(mut ys, 2)
list.extend(mut xs, ys)
list.len(xs) == 2
(list.last(xs) else 0) == 2

concat

fn · Source line 489

pub fn concat[T](a: List[T], b: List[T]) -> List[T]

A new list holding a's elements followed by b's; both operands are read, not consumed (to_-style allocation, §1). Total: no error row, no traps beyond allocation.

var a = List[int]()
var b = List[int]()
list.push(mut a, 1)
list.push(mut b, 2)
list.len(list.concat(a, b)) == 2

fill

fn · Source line 507

pub fn fill[T](mut xs: List[T], v: T)

Overwrites every element of xs with v, keeping its length. Total: no error row, no traps.

var xs = List[int]()
list.push(mut xs, 1)
list.push(mut xs, 2)
list.fill(mut xs, 9)
(list.first(xs) else 0) == 9
(list.last(xs) else 0) == 9

repeat

fn · Source line 525

pub fn repeat[T](xs: List[T], n: int) -> List[T]

A new list with xs's elements repeated n times; n <= 0 yields an empty list. Total: no error row, no traps beyond allocation.

var xs = List[int]()
list.push(mut xs, 1)
list.push(mut xs, 2)
list.len(list.repeat(xs, 3)) == 6

any

fn · Source line 566

pub fn any[T](xs: List[T], pred: fn(T) -> bool) -> bool

Does pred hold for ANY element? False on the empty list. Walks in order and stops at the first hit.

Call shape (witnessed by tests/list/predicate_tier.lu on all three lanes): fn odd(v: int) -> bool { v % 2 == 1 } … then list.any(xs, odd).

all

fn · Source line 576

pub fn all[T](xs: List[T], pred: fn(T) -> bool) -> bool

Does pred hold for EVERY element? Vacuously true on the empty list — the logician's all, and the one that makes all(xs, p) == !any(xs, not_p) an identity.

count_where

fn · Source line 585

pub fn count_where[T](xs: List[T], pred: fn(T) -> bool) -> int

How many elements satisfy pred. The whole walk, always — there is nothing to stop early for.

index_where

fn · Source line 597

pub fn index_where[T](xs: List[T], pred: fn(T) -> bool) -> int ! {none}

The index of the FIRST element satisfying pred; none when none does. The index answers "where", find_where answers "what" — two fns because a T is not an int and a sentinel index is §1's forbidden shape.

find_where

fn · Source line 607

pub fn find_where[T](xs: List[T], pred: fn(T) -> bool) -> T ! {none}

The first element satisfying pred; none when none does.

min_by

fn · Source line 620

pub fn min_by[T](xs: List[T], better: fn(T, T) -> bool) -> T ! {none}

The BEST element under better, where better(a, b) reads "a is better than b": the first element such that no later one beats it. none on the empty list. With better = "less than" this is the minimum; flip the relation and it is the maximum — max_by IS this fn with the arguments of better swapped in the caller's relation, documented rather than duplicated.

max_by

fn · Source line 636

pub fn max_by[T](xs: List[T], better: fn(T, T) -> bool) -> T ! {none}

min_by under the flipped relation, named for the call sites that read better with it. max_by(xs, gt) == min_by(xs, gt) when gt is already "greater is better"; the two names exist so neither caller writes a lambdaless flip by hand.

is_sorted_by

fn · Source line 644

pub fn is_sorted_by[T](xs: List[T], le: fn(T, T) -> bool) -> bool

Is xs sorted under le, where le(a, b) reads "a may precede b"? True for the empty and single-element lists. This is the witness sort_by will be judged by the day wolf-lang#23 frees the Ordering comparator; it lands beside that freight deliberately.

map

fn · Source line 712

pub fn map[T, U](xs: List[T], f: fn(T) -> U) -> List[U]

A new list holding f(x) for every x in xs, in order; xs is read, not consumed. The empty list maps to the empty list. Total: no error row, no traps beyond allocation and whatever f traps.

[type.comb.set] (wolf-lang 4c046f15): the method xs.map(f) is this call ([type.method.resolve] step 2), and eager ([type.comb.eager]). Cost, as the clause states it: one fresh List[U] of xs.len in the ambient region, and xs.len calls of f. A fallible f is par's alone ([conc.task.par]).

The one element here that this call OWNS, and the spelling it is owed. What this list holds is f's own return value, not an element moved out of xs, so nothing lent escapes ([mem.tier0.mode.read], wolf-lang#366); x is only LENT to f for the call, which is what a read parameter is for, and an f that hands its own argument back is the caller's site, not this one. Because f(x) is a temporary this activation owns, the right spelling under wolf-lang#385 (ruled option 3, s167, unreleased) is (mut out).push(take f(x)), which MOVES it where the default copies. It is not written yet because it does not exist yet: at wolf 0.2.14 both compiler rungs answer E1007 — push takes value as plain read, no mode is written for it (measured, F-0136), and that is a static refusal of this whole module, so every std.list row on both rungs would pay for it. It lands with the pin that carries s167, and the cost line above changes with it.

var xs = List[int]()
list.push(mut xs, 1)
list.push(mut xs, 2)
let ys = list.map(xs, fn(v) v * 10)
list.len(ys) == 2
(list.last(ys) else 0) == 20

filter

fn · Source line 754

pub fn filter[T](xs: List[T], keep: fn(T) -> bool) -> List[T]

A new list holding the elements of xs for which keep holds, in their original order; xs is read, not consumed. The in-place sibling (retain) is not written: a returning filter is the combinator, and a mut twin would be a second name for one walk. Total: no error row, no traps beyond allocation.

[type.comb.set] (wolf-lang 4c046f15): the method xs.filter(keep) is this call ([type.method.resolve] step 2). Cost, as the clause states it: one fresh List[T] of at most xs.len in the ambient region, and xs.len calls of keep, plus one element copy per kept element — push's own, not one spelled here.

**Why no copy is written.** xs is a read parameter, so an element moved out of it is LENT and may not escape this call ([mem.tier0.mode.read], wolf-lang#366, in 0.2.15) — and the kept elements do escape, in the returned list. That clause says in its own words that it does not reach the builtin store which keeps a read argument inside its receiver; that is wolf-lang#385, ruled option 3 (s167, unreleased): push COPIES a non-Copy element by default, and push(take v) moves it. The default copy is exactly the copy this function needs, so writing copy x here would buy a second one. push's copy is deep on the native tiers ([mem.tier0.move.3], wolf-lang#384): a List element copies its buffer, a str element shares its immutable bytes, a scalar costs nothing. Nothing is lent out of this module either way, at this pin or at the next.

var xs = List[int]()
list.push(mut xs, 1)
list.push(mut xs, 2)
list.push(mut xs, 3)
list.len(list.filter(xs, fn(v) v % 2 == 1)) == 2
(list.last(list.filter(xs, fn(v) v % 2 == 1)) else 0) == 3

fold

fn · Source line 782

pub fn fold[T, A](xs: List[T], init: A, step: fn(A, T) -> A) -> A

Combines every element of xs into one value, left to right: f(… f(f(init, x0), x1) …, xn). init is the answer for the empty list. The accumulator's type A need not be the element's. Total: no error row, no traps beyond whatever step traps.

[type.comb.set] (wolf-lang 4c046f15): the method xs.fold(init, step) is this call ([type.method.resolve] step 2). Cost, as the clause states it: no allocation of its own, and xs.len calls of step, left to right. The clause's f64 sum is this function: xs.fold(0.0, fn(a, x) a + x).

var xs = List[int]()
list.push(mut xs, 1)
list.push(mut xs, 2)
list.push(mut xs, 3)
list.fold(xs, 0, fn(acc, v) acc + v) == 6

sum

fn · Source line 816

pub fn sum(xs: List[int]) -> int

The sum of xs; 0 for the empty list. Traps overflow the moment a partial sum leaves int (checked arithmetic, X3), deliberately not a row: std.search.sum's contract, word for word.

[type.comb.set] (wolf-lang 4c046f15): the method xs.sum() is this call on a List[int], and on any other List it is E0403, not a match. Cost, as the clause states it: none beyond checked addition left to right (X3).

Monomorphic over int, and that is measured, not modest. A generic sum[T: ops.Add] needs use std.ops, which imports std.cmp, and every importer of std.cmp is unsupported on the native rung (the search/* rows carry exactly that), so the bound would cost every std.list importer a lane; std.ops has no zero for T either. The generic reduction is fold. std.search.sum stays: two modules may share a function name (a program importing both links at this pin, measured natively on list.len beside bytes.len), and this one is the three-lane spelling.

var xs = List[int]()
list.push(mut xs, 1)
list.push(mut xs, 2)
list.push(mut xs, 3)
list.sum(xs) == 6

enumerate

fn · Source line 848

pub fn enumerate[T](xs: List[T]) -> List[(int, T)]

A new list pairing each element of xs with its index, in order: (0, x0), (1, x1), …. The shape for (i, x) in list.enumerate(xs) destructures. Total: no error row, no traps beyond allocation.

[type.comb.set] (wolf-lang 4c046f15): the method xs.enumerate() is this call. Cost, as the clause states it: one fresh List of pairs, indices from 0, plus push's own copy of each pair — the pairs escape with the element inside them ([mem.tier0.mode.read], wolf-lang#366), and the store that copies them is wolf-lang#385's, ruled option 3 (s167, unreleased): push copies a non-Copy element by default, deeply on the native tiers ([mem.tier0.move.3], wolf-lang#384) — a scalar costs nothing, a str shares its bytes, a List element copies its buffer. No copy is spelled here, because that default is already it.

var xs = List[str]()
list.push(mut xs, "a")
list.push(mut xs, "b")
let pairs = list.enumerate(xs)
let second = pairs[1]
second.0 == 1
second.1 == "b"

zip

fn · Source line 887

pub fn zip[T, U](xs: List[T], ys: List[U]) -> List[(T, U)]

A new list pairing the elements of xs and ys position by position: (x0, y0), (x1, y1), …, as long as the SHORTER of the two, so the longer list's tail is dropped rather than trapped on. Both operands are read, not consumed. Built through the private fresh for the reason enumerate is (F-0133). Total: no error row, no traps beyond allocation.

[type.comb.set] (wolf-lang 4c046f15): the method xs.zip(ys) is this call. Cost, as the clause states it: one fresh List of min(xs.len, ys.len) pairs, plus push's own copy of each pair — BOTH lists are read parameters and both elements escape inside the pairs ([mem.tier0.mode.read], wolf-lang#366), and the copy that makes that legal is push's under wolf-lang#385, ruled option 3 (s167, unreleased), deep on the native tiers ([mem.tier0.move.3], wolf-lang#384). It stops at the shorter list because a length mismatch is not an access out of range.

var names = List[str]()
list.push(mut names, "ada")
list.push(mut names, "grace")
var scores = List[int]()
list.push(mut scores, 91)
let rows = list.zip(names, scores)
list.len(rows) == 1
var total = 0
for (n, s) in rows { total = total + s + n.len }
total == 94

sort_by

fn · Source line 937

pub fn sort_by[T](mut xs: List[T], less: fn(T, T) -> bool)

Sorts xs in place, stably, under less, where less(a, b) reads "a goes before b". Equal elements (neither goes before the other) keep their input order: the merge takes from the right run only when less(right, left) holds. less must be a strict weak order or the result is merely a permutation; std cannot check that and does not pretend to. Total: no error row, no traps beyond allocation and whatever less traps. std.sort's bottom-up merge, line for line. The result satisfies is_sorted_by(xs, fn(a, b) !less(b, a)).

[type.comb.set] (wolf-lang 4c046f15): the method (mut xs).sort_by(less) is this call, its receiver's mut spelled ([type.method.resolve], X1). Cost, as the clause states it: stable, O(n log n) calls of less, O(n) scratch in the ambient region, and no copy at all: the receiver is mut, not read, so no element here is lent and the sorted elements go back to the caller's own list ([mem.tier0.mode.read], wolf-lang#366). The returning twin sorted_by pays the copies instead, because its receiver is read.

**Why the relation is bool and why std.sort keeps its own.** The clause rules it: std.sort.sort_by takes a cmp.Ordering, and at wolf 0.2.14 / lupin 0.1.36 it runs on no lane (re-measured for this function: lupin declines the enum's dynamics at resolve, the checked machine declines std.cmp's module items, the native rung finds .is_lt() untyped; F-0029). std.sort's Ordering pair keeps its qualified spelling; the method is this one. Same mode, same stability, same algorithm; the comparator's type is the one difference, and it is stated on both.

Call shape (a prose example, witnessed by tests/list/sort_by_tier.lu on all three lanes): fn shorter(a: str, b: str) -> bool { a.len < b.len } … then list.sort_by(mut words, shorter). Not fenced: the call is a statement, and §4's extractor reads a line holding the relation's < as an assertion. sorted_by's example is fenced and shows the same order. Do not name a top-level fn less in a program that passes a different relation: the checked machine calls it instead of the argument (F-0134, wolf-lang#400).

sorted_by

fn · Source line 1019

pub fn sorted_by[T](xs: List[T], less: fn(T, T) -> bool) -> List[T]

sort_by's returning twin: a fresh list holding xs's elements in less's order, stably, with xs untouched. less must be a strict weak order or the result is merely a permutation. Total: no error row, no traps beyond allocation and whatever less traps.

[type.comb.set] (wolf-lang 4c046f15): the method xs.sorted_by(less) is this call, the chain's sort (corpus/ wordcount.lu's sorted_by). Cost, as the clause states it: sort_by over a fresh copy, plus push's own copy per element building that copy — the elements escape in the returned list and xs is a read parameter ([mem.tier0.mode.read], wolf-lang#366), and the store that copies them is wolf-lang#385's, ruled option 3 (s167, unreleased), deep on the native tiers ([mem.tier0.move.3], wolf-lang#384). std.sort.sorted_by is the Ordering twin that runs on no lane (F-0029) and keeps its qualified spelling.

var xs = List[int]()
list.push(mut xs, 3)
list.push(mut xs, 1)
list.push(mut xs, 2)
(list.first(list.sorted_by(xs, fn(a, b) a < b)) else 0) == 1
(list.last(list.sorted_by(xs, fn(a, b) a < b)) else 0) == 3
(list.first(xs) else 0) == 3

sorted

fn · Source line 1112

pub fn sorted[T: cmp.Ord](xs: List[T]) -> List[T]

A fresh list holding xs's elements in T's own ascending order, stably, with xs untouched. T: cmp.Ord is a total order by its own law, so this needs no relation from the caller. Total: no error row, no traps beyond allocation.

[type.comb.set] (wolf-lang 4c046f15): the method xs.sorted() is this call. Cost, as the clause states it: sorted_by with < ([type.trait.op]) — O(n log n) comparisons, O(n) scratch, plus push's own copy per element (#385 option 3, as the other combinators).

Its lanes, and the two rows behind them (F-0135). It ships at wolf-std 42d42b9 and not before: until sc49 rewrote std.cmp's Ordering impls without a product pattern, the use std.cmp above made EVERY importer of this module unsupported on the native rung, which is the "budget a module by its worst body" rule. The checked machine still declines this one function — "trait dispatch on a non-nominal receiver" at a < b, where T is bound through a list read (wolf-lang#402) — so tests/list/sorted_tier.lu is unsupported there and nothing else in the module pays for it. Prose, not a fenced example, for that reason: §4 wants a lane that refuses honestly, and lupin and native both run it — list.sorted(xs) over [3, 1, 2] answers 1, 2, 3.

Constants

No public declarations in this section.

Errors and row tags

Tags named by public signatures: none. Their conditions are documented on the functions that return them.

No public declarations in this section.