Function createIterator

  • Create an iterator which conforms to the Iterator protocol, it uses the provided ctx to managed moving to the next.

    Type Parameters

    • T

      Identifies the type that will be returned by the iterator

    Parameters

    Returns Iterator<T>

    A new Iterator instance

    0.4.2

    let idx = -1;
    let theValues = [ 5, 10, 15, 20, 25, 30 ];

    function getNextFn() {
    idx++;
    let isDone = idx >= theValues.length;
    if (!isDone) {
    // this is passed as the current iterator
    // so you can directly assign the next "value" that will be returned
    this.v = theValues[idx];
    }

    return isDone;
    }

    let theIterator = createIterator<number>({ n: getNextFn });

    let values: number[] = [];
    iterForOf(theIterator, (value) => {
    values.push(value);
    });

    // Values: [5, 10, 15, 20, 25, 30 ]