[Javascript] Generator & Iterators exercise

Generator can run with for .. of and ..., which will only emit yield values 

For example:

function* count() {
    yield 1;
    yield 2;
    return 3;
}

for (const value of count()) {
   console.log(value) // 1, 2
}


console.log([...count()]) // [1, 2]

 

Iterator

Using [Symbol.iterator]()which should return next() function:

const range = {
    from: 1,
    to: 5,
    [Symbol.iterator]() {
        return {
             current: this.from,
             last: this.to,
             next() {
                 if (this.current <= this.last) {
                     return {done: false, value: this.current++}
                 } else {
                     return {done: true}
                 }
             }
        }
    }
}

console.log([...range]) // [1,2,3,4,5]

 

Or we can also just do geneator appraoch:

const range = {
    from: 1,
    to: 5,
    *[Symbol.iterator]() {
        for (let i = this.from; i <= this.to; i++) {
           yield i
        }
    }
}

console.log([...range]) // [1,2,3,4,5]

 

One usecase for geneator and iterator is that not overload the data, everytime can just load a small tunck of data to process with next() call instead of loading the whole data into memory

 

Delegating a yield

function* gen1() {
    yield 2;
    yield 3;
}

function* gen2() {
    yield 1;
    yield* gen1() // delegating a yiled*, yield* must used with iterable yiled* [2,3] 
    yield 4;
}

console.log([...gen2()]) // [1,2,3,4]

 

posted @ 2024-03-13 19:42  Zhentiw  阅读(4)  评论(0编辑  收藏  举报