zena:iterator

zena
import {…} from 'zena:iterator';

Interfaces

Iterator

zena
interface Iterator<T>

A stateful iterator over elements of type T.

Usage pattern with let-pattern conditions:

text
let iter = collection.[Iterable.iterator]();
while (let (true, item) = iter.next()) {
  // use item
}

Design: Uses inline tuple returns for zero-allocation iteration.

  • next() returns inline (true, T) when there's an element
  • next() returns inline (false, _) when exhausted
Methods
zena
next(): inline (true, T) | inline (false, _)
#

Advances the iterator and returns the next element. Returns inline (true, element) if there is an element, inline (false, _) if exhausted.

Iterable

zena
interface Iterable<T>

A collection that can produce an Iterator.

Methods
zena
contains(value: T): boolean
#

Returns true if the collection contains the specified value.

zena
all(predicate: (item: T) => boolean): boolean
#

Returns true if all elements match the given predicate. Returns true if the collection is empty.

zena
some(predicate: (item: T) => boolean): boolean
#

Returns true if at least one element matches the given predicate. Returns false if the collection is empty.

zena
fold<R>(initial: R, combine: (acc: R, item: T) => R): R
#

Reduces the collection to a single value by accumulating state.

zena
find(predicate: (item: T) => boolean): inline (true, T) | inline (false, _)
#

Finds the first element that matches the predicate. Returns an inline tuple (true, item) if found, or (false, _) if not.

zena
filter(predicate: (item: T) => boolean): Iterable<T>
#

Returns a new iterable containing only the elements that match the predicate.