Library

std/iter

Lazy generator iterator combinators + terminals.

1 file lib/std/iter/iter.vader 27 exports
  • Combinators map / filter / take / skip / indexed / flat_map / chain / zip / window / chunked / distinct / filter_not_null are generators. arr.map(f) / it.indexed() read like a pipeline and dispatch via UFCS ; consumed by a for loop or a terminal (collect / count / any_match / ...), the chain fuses to a single flat loop: no intermediate arrays, no iterator struct, no next() dispatch (vader/lower/iter_fusion.vader). chain fuses two source loops ; zip stays T[]/U[] (its lockstep pull needs indexed access, unlike the source-driven combinators). Their tuple elements destructure in for [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_map take an Iterator<T> and consume it with one for (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 / last take a T[], in O(1). For a plain array prefer the built-ins : arr.len(), arr[start..<end], arr.clone() (not the removed slice / eager collect).

Functions

is_empty

fn 2 overloads
is_empty :: fn<T>(arr: T[]) -> bool

True when arr carries no elements.

Parameters

arrT[] source array.

Returns

bool true when arr.len() == 0.
is_empty :: fn<T>(it: Iterator<T>) -> bool

True when it yields nothing. Pulls one element at most, with no peek: a false result has consumed the first one.

Parameters

itIterator<T> source iterator.

Returns

bool

count

fn 2 overloads
count :: fn<T>(arr: T[]) -> i64

How many elements arr holds: its length, O(1).

Parameters

arrT[] source array.

Returns

i64
count :: fn<T>(it: Iterator<T>) -> i64

Drain it, returning how many elements it yielded: O(n).

Parameters

itIterator<T> source iterator.

Returns

i64

last

fn 2 overloads
last :: fn<T>(arr: T[]) -> T | null

The last element of arr, or null when it is empty: O(1).

Parameters

arrT[] source array.

Returns

T | null
last :: fn<T>(it: Iterator<T>) -> T | null

Drain it, returning its last element, or null when it yields none.

Parameters

itIterator<T> source iterator.

Returns

T | null
collect :: fn<T>(it: Iterator<T>) -> T[]

Drain it into a fresh array. Pair with the lazy combinators (it.filter(p).map(f).collect()) when a snapshot is required ; for x in it consumes directly without the intermediate array.

Parameters

itIterator<T> No description.

Returns

T[]
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

selfIterator<T> source iterator (or array, auto-coerced).
initU initial accumulator value.
ffn(U, T) -> U reducer ; takes the running accumulator and the next element.

Returns

U the accumulator after consuming the iterator.
sum :: fn<T: Add & Zero>(self: Iterator<T>) -> T

Sum the elements of an iterator (an array auto-coerces at the call site).

Parameters

selfIterator<T> the elements to add.

Returns

T their sum ; T.zero() when there are none.

Example

total :: [1.5, 2.25].sum()   // 3.75
product :: fn<T: Mul & One>(self: Iterator<T>) -> T

Multiply the elements of an iterator (an array auto-coerces at the call site).

Parameters

selfIterator<T> the elements to multiply.

Returns

T their product ; T.one() when there are none.

Example

total :: [2, 3, 4].product()   // 24
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

arrT[] 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

selfT[] left source array.
otherU[] 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

selfT[] source array.
sizei64 window width in elements ; size == 0 (or self.len() < size) yields nothing.

Returns

Iterator<T[]> a lazy Iterator<T[]>: self[0..<size], self[1..<size+1], ... while the window fits.

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

selfIterator<T> first source iterator (a raw array auto-coerces at the call site).
otherIterator<T> second source, streamed after self is exhausted.

Returns

Iterator<T> a lazy Iterator<T> over self followed by other.
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

selfIterator<T> source iterator (a raw array auto-coerces at the call site).

Returns

Iterator<[T, i64]> a lazy Iterator<[T, i64]>: [self[0], 0], [self[1], 1], …
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

selfIterator<T> source iterator (a raw array auto-coerces at the call site).
sizei64 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

selfIterator<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 2
filter_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

selfIterator<T | null> source iterator over a nullable element type.

Returns

Iterator<T> a lazy Iterator<T>: self with every null dropped.

Example

maybe: (i32 | null)[]: [1, null, 3]
for x in maybe.filter_not_null() { println("${x + 1}") }   // 2 4
flat_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

selfIterator<T> source iterator.
ffn(T) -> U[] mapper from one element to an array of outputs.

Returns

Iterator<U> an iterator over f(x0) then f(x1) then … , flattened.
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

itIterator<T> source array or iterator.
predfn(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

itIterator<T> source array or iterator.
predfn(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

itIterator<T> source array or iterator.
predfn(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

itIterator<T> source array or iterator.
ffn(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

selfIterator<T> No description.
predfn(T) -> bool No description.

Returns

map :: fn<T, U>(self: Iterator<T>, f: fn(T) -> U) -> Iterator<U>

Fluent lazy map. Mirror of filter for transforms ; element type can change (T → U).

Parameters

selfIterator<T> No description.
ffn(T) -> U No description.

Returns

take :: fn<T>(self: Iterator<T>, n: i64) -> Iterator<T>

Fluent lazy take. Yields the first n items from self, then ends: pulling exactly n items from self, none for take(0).

Parameters

selfIterator<T> No description.
ni64 No description.

Returns

skip :: fn<T>(self: Iterator<T>, n: i64) -> Iterator<T>

Fluent lazy skip. Drops the first n items from self, then forwards every subsequent item.

Parameters

selfIterator<T> No description.
ni64 No description.

Returns

Written by vader doc. Icons: Codicons, CC BY 4.0. Fonts: Atkinson Hyperlegible, JetBrains Mono, OFL.