std/iter
Lazy generator iterator combinators + terminals.
- Combinators
map/filter/take/skip/indexed/flat_map/chain/zip/window/chunked/distinct/filter_not_nullaregenerators.arr.map(f)/it.indexed()read like a pipeline and dispatch via UFCS ; consumed by aforloop or a terminal (collect/count/any_match/ ...), the chain fuses to a single flat loop: no intermediate arrays, no iterator struct, nonext()dispatch (vader/lower/iter_fusion.vader).chainfuses two source loops ;zipstaysT[]/U[](its lockstep pull needs indexed access, unlike the source-driven combinators). Their tuple elements destructure infor [x, y] in. The generator's state machine only materialises when the value escapes. - Terminals
collect/fold/sum/count/last/is_empty/any_match/all_match/find/find_maptake anIterator<T>and consume it with onefor(a raw array auto-coerces at the call site) ; over a chain, the call inlines into the fused loop. - Array helpers
clone/is_empty/count/lasttake aT[], in O(1). For a plain array prefer the built-ins :arr.len(),arr[start..<end],arr.clone()(not the removedslice/ eagercollect).
Functions
is_empty :: fn<T>(arr: T[]) -> bool True when arr carries no elements.
Parameters
| arr | T[] | source array. |
Returns
bool true when arr.len() == 0.count :: fn<T>(arr: T[]) -> i64 How many elements arr holds: its length, O(1).
Parameters
| arr | T[] | source array. |
Returns
i64 last :: fn<T>(arr: T[]) -> T | null The last element of arr, or null when it is empty: O(1).
Parameters
| arr | T[] | source array. |
Returns
T | null fold :: fn<T, U>(self: Iterator<T>, init: U, f: fn(U, T) -> U) -> U Left-fold the iterator with seed init. f(acc, x) is called for every
element in order ; the final accumulator is returned. A raw array coerces to
Iterator<T> at the call site (arr.fold(0, add) / fold(arr, 0, add)).
Parameters
| self | Iterator<T> | source iterator (or array, auto-coerced). |
| init | U | initial accumulator value. |
| f | fn(U, T) -> U | reducer ; takes the running accumulator and the next element. |
Returns
U the accumulator after consuming the iterator.clone :: fn<T>(arr: T[]) -> T[] Fresh copy of arr. The returned array is independent: mutating it does
not affect arr.
Use this to modify what a frozen value holds (a module constant, a view of a string's bytes), or to break aliasing between a slice view and its parent.
Naming overlaps with the Clone trait in std/core (currently
without implementors). When T[] implements Clone lands, this free
fn becomes the impl: arr.clone() resolves identically either way.
Parameters
| arr | T[] | No description. |
Returns
T[] zip :: fn<T, U>(self: T[], other: U[]) -> Iterator<[T, U]> Lazily pair elements of self and other into tuples, stopping at the
shorter of the two. Mirrors Python zip ; useful for stepping parallel
arrays in lockstep (e.g. keys + values from a map). Both inputs stay T[]/
U[]: the lockstep pull needs indexed access, so unlike the source-driven
combinators zip does not take a lazy Iterator. for [x, y] in zip(a, b)
destructures each pair ; consumed directly it fuses to a single index loop.
Parameters
| self | T[] | left source array. |
| other | U[] | right source array. |
Returns
Iterator<[T, U]> a lazy Iterator([T, U]): (self[0], other[0]), ... capped at min(self.len(), other.len()).window :: fn<T>(self: T[], size: i64) -> Iterator<T[]> Slide a fixed-size window over self, yielding each run of size consecutive
elements. Step 1, no partial windows: a source shorter than size yields
nothing. Mirrors Kotlin's windowed(size). Like zip, takes T[] (not a lazy
Iterator) because the overlapping pull needs indexed access.
Each window is a zero-copy VIEW aliasing self (the built-in arr[lo..<hi]
slice): cheap, but a later mutation of self shows through ; .clone() a
window to detach it.
Parameters
| self | T[] | source array. |
| size | i64 | window width in elements ; size == 0 (or self.len() < size) yields nothing. |
Returns
Example
for w in [1, 2, 3, 4, 5].window(3) { println("${w}") } // [1,2,3] [2,3,4] [3,4,5]chain :: fn<T>(self: Iterator<T>, other: Iterator<T>) -> Iterator<T> Lazily yield every element of self, then every element of other: a
concatenated stream. Fuses to two back-to-back source loops (no intermediate
array) when consumed by a for loop or a terminal.
Parameters
| self | Iterator<T> | first source iterator (a raw array auto-coerces at the call site). |
| other | Iterator<T> | second source, streamed after self is exhausted. |
Returns
indexed :: fn<T>(self: Iterator<T>) -> Iterator<[T, i64]> Lazily pair each element with its index, starting at 0: the element first,
so for [x, i] in it.indexed() { … } reads in the order it is yielded. A
0-arg combinator: fuses over the source with no intermediate array.
Parameters
| self | Iterator<T> | source iterator (a raw array auto-coerces at the call site). |
Returns
chunked :: fn<T>(self: Iterator<T>, size: i64) -> Iterator<T[]> Split self into consecutive runs of size elements ; the last run holds
what remains and may be shorter. Mirrors Kotlin's chunked(size).
Unlike window, each chunk is a FRESH array, not a view: the source is a
lazy Iterator, so there is nothing to alias.
Parameters
| self | Iterator<T> | source iterator (a raw array auto-coerces at the call site). |
| size | i64 | elements per chunk ; size == 0 yields nothing. |
Returns
Iterator<T[]> a lazy Iterator<T[]>: [x0, …, x(size-1)], then the next run, … , then the shorter remainder if any.Example
for c in [1, 2, 3, 4, 5].chunked(2) { println("${c}") } // [1, 2] [3, 4] [5]distinct :: fn<T: Hash & Equals>(self: Iterator<T>) -> Iterator<T> Lazily yield each element of self the first time it occurs, in order of
first occurrence. Mirrors Kotlin's distinct().
Remembers every element seen so far, so memory grows with the number of DISTINCT elements, not with the length of the stream.
Parameters
| self | Iterator<T> | source iterator (a raw array auto-coerces at the call site). |
Returns
Iterator<T> a lazy Iterator<T> with every repeat dropped.Example
for x in [3, 1, 3, 2, 1].distinct() { println("${x}") } // 3 1 2filter_not_null :: fn<T>(self: Iterator<T | null>) -> Iterator<T> Lazily yield the non-null elements of self, typed without the null.
Mirrors Kotlin's filterNotNull().
Parameters
| self | Iterator<T | null> | source iterator over a nullable element type. |
Returns
Example
maybe: (i32 | null)[]: [1, null, 3]
for x in maybe.filter_not_null() { println("${x + 1}") } // 2 4flat_map :: fn<T, U>(self: Iterator<T>, f: fn(T) -> U[]) -> Iterator<U> Apply f to each element and yield the elements of every resulting array in
order. Useful when one input expands to zero-or-more outputs (parsing, tree
flattening).
Parameters
| self | Iterator<T> | source iterator. |
| f | fn(T) -> U[] | mapper from one element to an array of outputs. |
Returns
any_match :: fn<T>(it: Iterator<T>, pred: fn(T) -> bool) -> bool true as soon as any element satisfies pred ; false if none do.
Short-circuits: pred is not invoked past the first match.
Parameters
| it | Iterator<T> | source array or iterator. |
| pred | fn(T) -> bool | predicate invoked on each element until one returns true. |
Returns
bool all_match :: fn<T>(it: Iterator<T>, pred: fn(T) -> bool) -> bool true only when every element satisfies pred. Short-circuits on the
first false. Returns true on an empty source (vacuous truth).
Parameters
| it | Iterator<T> | source array or iterator. |
| pred | fn(T) -> bool | predicate invoked on each element until one returns false. |
Returns
bool find :: fn<T>(it: Iterator<T>, pred: fn(T) -> bool) -> T | null First element matching pred, or null if none does. Short-circuits.
Consumers narrow naturally : match hit :: arr.find(p) { is T -> … }.
Parameters
| it | Iterator<T> | source array or iterator. |
| pred | fn(T) -> bool | predicate invoked on each element until one returns true. |
Returns
T | null find_map :: fn<T, U>(it: Iterator<T>, f: fn(T) -> U | null) -> U | null Apply f to each element ; return the first non-null result. Combines
the transform and the search in one pass: useful for parsing chains
where the mapper itself decides whether an element qualifies.
Parameters
| it | Iterator<T> | source array or iterator. |
| f | fn(T) -> U | null | mapper that returns null to skip the element or a value to short-circuit on. |
Returns
U | null filter :: fn<T>(self: Iterator<T>, pred: fn(T) -> bool) -> Iterator<T> Fluent lazy filter. Distinct from the eager T[]-overload above:
the resolver picks this one when the receiver implements Iterator<T>
(Range, an array iterator, MutableMap, …).
Parameters
| self | Iterator<T> | No description. |
| pred | fn(T) -> bool | No description. |
Returns
Iterator<T>