unfold
Builds an array from a seed value using an iterator function.
The iterator receives the current seed and returns either a [value, nextSeed]
tuple to continue, or false to stop.
Dual of reduce — reduce collapses a list into a value, unfold expands
a value into a list.
Termination is the caller's responsibility. If fn never returns
false, unfold will run until memory is exhausted. For bounded
generation, encode a counter or limit into the seed.
Usage
unfold<T, R>(fn: (seed: T) => [R, T] | false, seed: T)
Builds an array from a seed value using an iterator function.
The iterator receives the current seed and returns either a [value, nextSeed]
tuple to continue, or false to stop.
Dual of reduce — reduce collapses a list into a value, unfold expands
a value into a list.
Termination is the caller's responsibility. If fn never returns
false, unfold will run until memory is exhausted. For bounded
generation, encode a counter or limit into the seed.
Parameters
the function to apply.
the initial seed value.
Returns
the generated values, ending when fn returns false.