How to clone an Iterator in javascript?

2019-08-03 08:57发布

问题:

In ES6, is there any possible to clone an iterator states?

var ma=[1,2,3,4];
var it=ma[Symbol.iterator]();
it.next();

if I want to remember here the it states how should I do in javascritp?

what is remebered in it? since the

JSON.stringify(it) //it would just return {}

回答1:

You can’t clone an arbitrary iterator, but you can create many distinct iterators from one by holding onto some state:

function tee(iterable) {
    const source = iterable[Symbol.iterator]();
    const buffers = [[], []];  // substitute in queue type for efficiency
    const DONE = Object.create(null);

    const next = i => {
        if (buffers[i].length !== 0) {
            return buffers[i].shift();
        }

        const x = source.next();

        if (x.done) {
            return DONE;
        }

        buffers[1 - i].push(x.value);
        return x.value;
    };

    return buffers.map(function* (_, i) {
        for (;;) {
            const x = next(i);

            if (x === DONE) {
                break;
            }

            yield x;
        }
    });
}

Usage:

const [a, b] = tee(iterator);
assert(a.next().value === b.next().value);


回答2:

It's not possible to clone an iterator. Iterator state is basically completely arbitrary and any given iterator may require or produce side effects (e.g. reading from or writing to a network stream) which are not repeatable on demand.