UNPKG

1.39 kBJavaScriptView Raw
1import { IterableX } from './iterablex';
2class GenerateIterable extends IterableX {
3 constructor(initialState, condition, iterate, resultSelector) {
4 super();
5 this._initialState = initialState;
6 this._condition = condition;
7 this._iterate = iterate;
8 this._resultSelector = resultSelector;
9 }
10 *[Symbol.iterator]() {
11 for (let i = this._initialState; this._condition(i); i = this._iterate(i)) {
12 yield this._resultSelector(i);
13 }
14 }
15}
16/**
17 * Generates an iterable sequence by running a state-driven loop producing the sequence's elements.
18 *
19 * @export
20 * @template TState The type of the state used in the generator loop.
21 * @template TResult The type of the elements in the produced sequence.
22 * @param {TState} initialState The initial state.
23 * @param {((value: TState) => boolean)} condition Condition to terminate generation (upon returning false).
24 * @param {((value: TState) => TState)} iterate Iteration step function.
25 * @param {((value: TState) => TResult)} resultSelector Selector function for results produced in
26 * the sequence.
27 * @returns {IterableX<TResult>} The generated iterable sequence.
28 */
29export function generate(initialState, condition, iterate, resultSelector) {
30 return new GenerateIterable(initialState, condition, iterate, resultSelector);
31}
32
33//# sourceMappingURL=generate.mjs.map