Trait Foldablepub
Source:trait(pub) Foldable
.fold ( (Self elem) acc [acc elem → acc ] → acc )
.for-each ( (Self elem) [elem → ] → )
.any ( (Self elem) [elem → Bool ] → Bool ) ;
.all ( (Self elem) [elem → Bool ] → Bool ) ;
.count ( (Self elem) → AnyInt ) ;
.first ( (Self elem) → (Option elem) ) ;
.last ( (Self elem) → (Option elem) ) ;
.find ( (Self elem) [elem → Bool ] → (Option elem) ) ;
.reduce ( (Self elem) [elem elem → elem ] → (Option elem) ) ;
.find-map ( (Self elem) [elem → (Option b) ] → (Option b) ) ;
.try-fold ( (Self elem) acc [acc elem → (Result acc err) ] → (Result acc err) ) ;
.try-for-each ( (Self elem) [elem → (Result Unit err) ] → (Result Unit err) ) ;
.try-reduce ( (Self elem) [elem elem → (Result elem err) ] → (Result (Option elem) err) ) ;
.min-by ( (Self elem) [elem elem → Ordering ] → (Option elem) ) ;
.max-by ( (Self elem) [elem elem → Ordering ] → (Option elem) ) ;
.enumerate ( (Self elem) → (Pair AnyInt (Vec (Pair AnyInt elem))) ) ;
.position ( (Self elem) [elem → Bool ] → (Option AnyInt) ) ;
.take ( (Self elem) AnyInt → (Vec elem) ) ;
.skip ( (Self elem) AnyInt → (Vec elem) ) ;
.partition ( (Self elem) [elem → Bool ] → (Vec elem la) (Vec elem lb) ) ;
.take-while ( (Self elem) [elem → Bool ] → (Vec elem) ) ;
.skip-while ( (Self elem) [elem → Bool ] → (Vec elem) ) ;
.min-by-key ( (Self elem) [elem → key ] → (Option elem) ) ;
.max-by-key ( (Self elem) [elem → key ] → (Option elem) ) ;
.count-where ( (Self elem) [elem → Bool ] → AnyInt ) ;
.filter-map ( (Self elem) [elem → (Option b) ] → (Vec b) ) ;
.map-while ( (Self elem) [elem → (Option b) ] → (Vec b) ) ;
.is-partitioned ( (Self elem) [elem → Bool ] → Bool ) ;
.scan ( (Self elem) state [state elem → state (Option result) ] → (Vec result) ) ;
.step-by ( (Self elem) AnyInt → (Vec elem) ) ;
.rposition ( (Self elem) [elem → Bool ] → (Option AnyInt) ) ;
.chunks-n ( (Self elem) AnyInt → (Vec (Vec elem)) ) ;
.intersperse ( (Self elem) elem → (Vec elem) ) ;
.unzip ( (Self (Pair a b)) → (Pair (Vec a) (Vec b)) ) ;
.flatten ( (Self (Vec elem n)) → (Vec elem) ) ;
.flat-map ( (Self elem) [elem → (Vec b n) ] → (Vec b) ) ;
Description
Trait for collections whose elements can be consumed by folding. The most general collection surface: any type that can produce its elements one at a time implements Foldable. .fold and .for-each are required; .any, .all, .count are defaults derived from .fold.
Required Methods
Provided Methods
.any ( (Self elem) [elem → Bool ] → Bool )default
.any ( (Self elem) [elem → Bool ] → Bool )default
Return true if pred holds for any element. ( (Self elem) [elem -> Bool] -> Bool )
.all ( (Self elem) [elem → Bool ] → Bool )default
.all ( (Self elem) [elem → Bool ] → Bool )default
Return true if pred holds for every element. ( (Self elem) [elem -> Bool] -> Bool )
.first ( (Self elem) → (Option elem) )default
.first ( (Self elem) → (Option elem) )default
Return Some elem for the first element, else None on empty. Not spec'd but a natural Foldable default. ( (Self elem) -> (Option elem) )
.last ( (Self elem) → (Option elem) )default
.last ( (Self elem) → (Option elem) )default
Return Some elem for the last element, else None on empty. Not spec'd but a natural Foldable default. ( (Self elem) -> (Option elem) )
.find ( (Self elem) [elem → Bool ] → (Option elem) )default
.find ( (Self elem) [elem → Bool ] → (Option elem) )default
Return Some elem for the first element satisfying pred, else None. No short-circuit — visits every element; keeps the first match. ( (Self elem) [elem -> Bool] -> (Option elem) )
.reduce ( (Self elem) [elem elem → elem ] → (Option elem) )default
.reduce ( (Self elem) [elem elem → elem ] → (Option elem) )default
Reduce the collection with a binary combiner. Returns None on empty. ( (Self elem) [elem elem -> elem] -> (Option elem) )
.find-map ( (Self elem) [elem → (Option b) ] → (Option b) )default
.find-map ( (Self elem) [elem → (Option b) ] → (Option b) )default
Return Some b for the first quotation call that yields Some b. Produces None if every element maps to None. ( (Self elem) [elem -> (Option b)] -> (Option b) )
.try-fold ( (Self elem) acc [acc elem → (Result acc err) ] → (Result acc err) )default
.try-fold ( (Self elem) acc [acc elem → (Result acc err) ] → (Result acc err) )default
Short-circuiting fold. Threads the accumulator through a combiner returning (Result acc err); stops at the first Err and returns it unchanged. On empty, returns init Ok. ( (Self elem) acc [acc elem -> (Result acc err)] -> (Result acc err) )
.try-for-each ( (Self elem) [elem → (Result Unit err) ] → (Result Unit err) )default
.try-for-each ( (Self elem) [elem → (Result Unit err) ] → (Result Unit err) )default
Short-circuiting for-each. Calls an effectful quotation returning (Result Unit err) per element; stops at the first Err. On an empty collection or all-Ok walk, returns unit Ok. ( (Self elem) [elem -> (Result Unit err)] -> (Result Unit err) )
.try-reduce ( (Self elem) [elem elem → (Result elem err) ] → (Result (Option elem) err) )default
.try-reduce ( (Self elem) [elem elem → (Result elem err) ] → (Result (Option elem) err) )default
Short-circuiting reduce. Empty collection yields None Ok. The first element seeds the reduction; subsequent elements are combined with the current value via f, whose (Result elem err) result short-circuits on Err. ( (Self elem) [elem elem -> (Result elem err)] -> (Result (Option elem) err) )
.min-by ( (Self elem) [elem elem → Ordering ] → (Option elem) )default
.min-by ( (Self elem) [elem elem → Ordering ] → (Option elem) )default
Return Some elem for the minimum element per comparator, else None. ( (Self elem) [elem elem -> Ordering] -> (Option elem) )
.max-by ( (Self elem) [elem elem → Ordering ] → (Option elem) )default
.max-by ( (Self elem) [elem elem → Ordering ] → (Option elem) )default
Return Some elem for the maximum element per comparator, else None. ( (Self elem) [elem elem -> Ordering] -> (Option elem) )
.enumerate ( (Self elem) → (Pair AnyInt (Vec (Pair AnyInt elem))) )default
.enumerate ( (Self elem) → (Pair AnyInt (Vec (Pair AnyInt elem))) )default
Pair each element with its zero-based index. Fold with a (Pair AnyInt (Vec (Pair AnyInt elem))) accumulator: .a tracks the next index, .b collects Pair idx elem entries. Returns the accumulator Pair as-is (index on .a, entries on .b); projecting .b inline in the body trips the checker on a spurious elem = Vec (Pair AnyInt elem) self-reference, and projecting at the call site also loses type info. Callers that need just the Vec bind with pop-> and project then. ( (Self elem) -> (Pair AnyInt (Vec (Pair AnyInt elem))) )
.position ( (Self elem) [elem → Bool ] → (Option AnyInt) )default
.position ( (Self elem) [elem → Bool ] → (Option AnyInt) )default
Return Some AnyInt for the zero-based index of the first element satisfying pred, else None. Fold with a (Pair AnyInt (Option AnyInt)) accumulator. ( (Self elem) [elem -> Bool] -> (Option AnyInt) )
.take ( (Self elem) AnyInt → (Vec elem) )default
.take ( (Self elem) AnyInt → (Vec elem) )default
Return the first n elements as a Vec. Fold with a (Pair AnyInt (Vec elem)) accumulator; once the collected count reaches n, subsequent elements are ignored. ( (Self elem) AnyInt -> (Vec elem) )
.skip ( (Self elem) AnyInt → (Vec elem) )default
.skip ( (Self elem) AnyInt → (Vec elem) )default
Return all elements after the first n. Fold with a (Pair AnyInt (Vec elem)) accumulator; elements are pushed only once the seen-count has reached n. ( (Self elem) AnyInt -> (Vec elem) )
.partition ( (Self elem) [elem → Bool ] → (Vec elem la) (Vec elem lb) )default
.partition ( (Self elem) [elem → Bool ] → (Vec elem la) (Vec elem lb) )default
Split elements by a predicate into two Vecs: the deeper Vec contains elements where pred returned true, the top Vec contains the rest. Matches spec: two Seqs on stack. ( (Self elem) [elem -> Bool] -> (Vec elem la) (Vec elem lb) )
.take-while ( (Self elem) [elem → Bool ] → (Vec elem) )default
.take-while ( (Self elem) [elem → Bool ] → (Vec elem) )default
Take elements from the start as long as pred returns true. The first element for which pred returns false stops collection — no later elements are included, even if they would match. Fold with a (Pair Bool (Vec elem)) accumulator: .a is the still- taking flag, .b is the collected Vec. Once the flag flips false it stays false. ( (Self elem) [elem -> Bool] -> (Vec elem) )
.skip-while ( (Self elem) [elem → Bool ] → (Vec elem) )default
.skip-while ( (Self elem) [elem → Bool ] → (Vec elem) )default
Skip elements from the start as long as pred returns true. Once pred returns false for some element, that element and every element after it are collected regardless of the predicate. Fold with a (Pair Bool (Vec elem)) accumulator: .a is the still-skipping flag, .b is the collected Vec. ( (Self elem) [elem -> Bool] -> (Vec elem) )
.min-by-key ( (Self elem) [elem → key ] → (Option elem) )default
.min-by-key ( (Self elem) [elem → key ] → (Option elem) )default
Return Some elem for the element with the smallest key extracted by f, else None on an empty collection. Fold with an (Option (Pair elem key)) accumulator so the .a / .b field projections pin the tuple shape through the fold. Uses .< on keys — the (Ord key key) constraint must supply it. ( (Self elem) [elem -> key] -> (Option elem) ) { (Ord key key) }
.max-by-key ( (Self elem) [elem → key ] → (Option elem) )default
.max-by-key ( (Self elem) [elem → key ] → (Option elem) )default
Return Some elem for the element with the largest key extracted by f, else None on an empty collection. Mirrors .min-by-key but replaces .< with .>. ( (Self elem) [elem -> key] -> (Option elem) ) { (Ord key key) }
.count-where ( (Self elem) [elem → Bool ] → AnyInt )default
.count-where ( (Self elem) [elem → Bool ] → AnyInt )default
Count the elements for which pred returns true. Pure fold with an integer counter. Not specced as its own iter rule — this is a natural Foldable default that sits between .count (unconditional) and .any/.all (boolean aggregation). ( (Self elem) [elem -> Bool] -> AnyInt )
.filter-map ( (Self elem) [elem → (Option b) ] → (Vec b) )default
.filter-map ( (Self elem) [elem → (Option b) ] → (Vec b) )default
Fused filter+map: call f on each element and collect the Some payloads into a Vec, skipping Nones. Wraps the output Vec in a single-field Pair-like accumulator (the unit pairing) so the checker pins the element type through fold — bare (Vec .default) as the fold acc leaves the element polymorphic and .push fails to resolve at install. ( (Self elem) [elem -> (Option b)] -> (Vec b) )
.map-while ( (Self elem) [elem → (Option b) ] → (Vec b) )default
.map-while ( (Self elem) [elem → (Option b) ] → (Vec b) )default
Like .filter-map, but the first None result stops iteration; subsequent elements are not consulted. Fold with a (Pair Bool (Vec b)) accumulator — .a is the still-taking flag, .b is the collected Vec. ( (Self elem) [elem -> (Option b)] -> (Vec b) )
.is-partitioned ( (Self elem) [elem → Bool ] → Bool )default
.is-partitioned ( (Self elem) [elem → Bool ] → Bool )default
Return true if every element for which pred returns true appears before every element for which it returns false. An empty collection is considered partitioned. Fold with a (Pair Bool Bool) accumulator: .a is the still-ok flag, .b records whether any false-predicate element has been observed yet. Seeing a true after a false breaks the invariant. ( (Self elem) [elem -> Bool] -> Bool )
.scan ( (Self elem) state [state elem → state (Option result) ] → (Vec result) )default
.scan ( (Self elem) state [state elem → state (Option result) ] → (Vec result) )default
Stateful transform. Thread an initial state through a stepper [state elem -> state (Option result)]. Each Some r contributes r to the output Vec; the first None stops iteration. Fold with a (Pair (Option state) (Vec result)) accumulator — outer .a is Some state while running and None once terminated, outer .b is the collected output. The :match on .a pins the Option shape through fold so the body's field dispatch and .push on the Vec resolve. ( (Self elem) state [state elem -> state (Option result)] -> (Vec result) )
.step-by ( (Self elem) AnyInt → (Vec elem) )default
.step-by ( (Self elem) AnyInt → (Vec elem) )default
Return every stride-th element starting with the first (index 0). Fold with a (Pair AnyInt (Vec elem)) accumulator: .a is the running element index, .b is the collected output. An element is included when i mod stride = 0. A stride of 1 returns every element; the spec flags stride=0 as a compile-time error — this default doesn't enforce that at the trait level. ( (Self elem) AnyInt -> (Vec elem) )
.rposition ( (Self elem) [elem → Bool ] → (Option AnyInt) )default
.rposition ( (Self elem) [elem → Bool ] → (Option AnyInt) )default
Return Some AnyInt for the zero-based index of the last element satisfying pred, else None. Fold visits every element, overwriting the tracked match-index each time the predicate holds. Accumulator is (Pair AnyInt (Option AnyInt)): .a is the running index, .b is the most recent matching index (or None). ( (Self elem) [elem -> Bool] -> (Option AnyInt) )
.chunks-n ( (Self elem) AnyInt → (Vec (Vec elem)) )default
.chunks-n ( (Self elem) AnyInt → (Vec (Vec elem)) )default
Partition a Foldable into fixed-size chunks. The last chunk MAY have fewer than n elements if the input length is not a multiple of n. Default is a stub; real body lives in the Foldable (Vec elem | len) impl override where the element type and outer accumulator are concretely bound. ( (Self elem) AnyInt -> (Vec (Vec elem)) )
.intersperse ( (Self elem) elem → (Vec elem) )default
.intersperse ( (Self elem) elem → (Vec elem) )default
Insert sep between every adjacent pair of input elements. An input with fewer than two elements MUST NOT include the separator. Fold with a (Pair Bool (Vec elem)) accumulator: .a tracks whether the next element is the first (no leading separator); .b is the collected output. The first element is pushed alone, subsequent elements are pushed after a separator. ( (Self elem) elem -> (Vec elem) )
.unzip ( (Self (Pair a b)) → (Pair (Vec a) (Vec b)) )default
.unzip ( (Self (Pair a b)) → (Pair (Vec a) (Vec b)) )default
Split a Foldable of Pair a b into two Vecs of the .a / .b components. Default is a stub; real body lives in the Foldable (Vec elem | len) impl override where the element is concretely Pair-shaped. ( (Self (Pair a b)) -> (Pair (Vec a) (Vec b)) )