Skip to content

std/array

std/array — Array.X receiver-method bodies. The checker auto-discovers any __method_Array_<name> function and registers it as Array.<name> for dispatch (internal/checker/checker.go). That’s the same mechanism the prelude has used since Phase 2; the only thing moving in this carve-out is where the bodies live. Methods are migrated here from the auto-injected prelude per docs/PRELUDE-TO-MODULES.md. The auto-prelude declares import "std/array"; so existing programs that call arr.sum() / arr.join(",") / etc. continue to work unchanged during the transition.

pub function sum_i64(arr: i64[]): i64

sum_i64 — total of every element. Wraps on overflow; users needing exact long sums should widen the per-element type first.

pub function max_i64(arr: i64[]): Option[i64]
pub function min_i64(arr: i64[]): Option[i64]
pub function avg_i64(arr: i64[]): Option[i64]

avg_i64 — integer mean (floor toward zero for positive sums, toward zero for negative — Go’s int64 division semantics). Returns None on empty.

pub function sum_u32(arr: u32[]): u32
pub function max_u32(arr: u32[]): Option[u32]
pub function min_u32(arr: u32[]): Option[u32]
pub function sum_u64(arr: u64[]): u64
pub function max_u64(arr: u64[]): Option[u64]
pub function min_u64(arr: u64[]): Option[u64]
pub function sum_f64(arr: f64[]): f64
pub function max_f64(arr: f64[]): Option[f64]
pub function min_f64(arr: f64[]): Option[f64]
pub function avg_f64(arr: f64[]): Option[f64]
pub function map[T, U](xs: T[], f: (T) => U): U[]

map(xs, f) — fresh U[] with f applied to every element. Empty → empty. The workhorse transform.

pub function filter[T](xs: T[], keep: (T) => boolean): T[]

filter(xs, keep) — fresh T[] of the elements for which keep returns true, original order preserved. Empty → empty.

pub function fold[T, A](xs: T[], init: A, f: (A, T) => A): A

fold(xs, init, f) — left fold. Threads acc (starting at init) through f(acc, x) for each element, returning the final accumulator. The general reduction; sum / product / min / max are all special cases.

pub function any[T](xs: T[], pred: (T) => boolean): boolean

any(xs, pred) — true if pred holds for at least one element. Short-circuits on the first match. Empty → false.

pub function all[T](xs: T[], pred: (T) => boolean): boolean

all(xs, pred) — true if pred holds for every element. Short-circuits on the first failure. Empty → true (vacuous).

pub function find[T](xs: T[], pred: (T) => boolean): Option[T]

find(xs, pred)Some(x) for the first element matching pred, else None. Short-circuits. The Option return keeps “not found” out-of-band rather than leaning on a sentinel.

pub function enumerate[T](xs: T[]): (i32, T)[]

enumerate(xs) — pair each element with its 0-based index: [(0, xs[0]), (1, xs[1]), ...]. Lets a single for (i, x) in enumerate(xs)-style loop carry the index without a manual counter. Empty → empty.