Skip to content

unfold

unfold<S, T>(seed, next): IterableIterator<T>

Defined in: packages/core/src/iterable/unfold.ts:19

Generates a sequence from a seed, one step at a time — the dual of reduce, which collapses a sequence into a single value.

next returns the value to yield paired with the state to carry forward, or undefined to stop. range is one instance of this shape, and is kept separate because a counter deserves a name rather than a closure.

Time complexity: O(1) per element, plus whatever next costs. Nothing is buffered, and a next that never stops yields forever.

S

T

S

The initial state.

Callable<[S], readonly [T, S] | undefined>

Receives the current state and returns [value, nextState], or undefined to end the sequence.

IterableIterator<T>

An iterator over the generated values.