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) { ... }