createIterable()
createIterable<
T>(factory):Iterable<T>
Creates a lazy, re-iterable sequence from a factory that produces a "next" function.
The factory is called each time [Symbol.iterator]() is invoked, ensuring
the iterable can be traversed multiple times. Each "next" call returns
Some(value) for the next element or None to signal completion.
This is the core of the GoF Iterator pattern: it decouples traversal logic
(the next function) from the data source.
Type Parametersβ
T: Tβ
The element type
Parametersβ
factory: () => () => Option<T>β
A function that creates a stateful "next" supplier
Returns: Iterable<T>β
A lazy Iterable
Sinceβ
2.4.0
Exampleβ
// Iterate over a tree in depth-first order
const treeIter = createIterable(() => {
const stack = [rootNode];
return () => {
if (stack.length === 0) return none;
const node = stack.pop();
if (!node) return none;
stack.push(...node.children);
return some(node.value);
};
});
for (const value of treeIter) { ... }