javascriptgeneratorecmascript-6

How to get the nth value of a JavaScript generator?


How can I get the nth value of a generator?

function *index() {
  let x = 0;
  while(true)
    yield x++;
}

// the 1st value
let a = index();
console.log(a.next().value); // 0

// the 3rd value
let b = index();
b.next();
b.next();
console.log(b.next().value); // 2

// the nth value?
let c = index();
let n = 10;
console.log(...); // 9

Solution

  • I wanted to avoid unnecessary creation of arrays or other intermediate values. Here's what my implementation of nth ended up like -

    function nth (iter, n)
    { for (const v of iter)
        if (--n < 0)
          return v
    }
    

    Following the examples in the original question -

    // the 1st value
    console.log(nth(index(), 0))
    
    // the 3rd value
    console.log(nth(index(), 2))
    
    // the 10th value
    console.log(nth(index(), 9))
    
    0
    2
    9
    

    For finite generators, if the index is out of bounds the result will be undefined -

    function* foo ()
    { yield 1
      yield 2
      yield 3
    }
    
    console.log(nth(foo(), 99))
    
    undefined
    

    Expand the snippet below to verify the results in your browser -

    function *index ()
    { let x = 0
      while (true)
        yield x++
    }
    
    function* foo ()
    { yield 1
      yield 2
      yield 3
    }
    
    function nth (iter, n) {
      for (const v of iter)
        if (--n < 0)
          return v
    }
    
    // the 1st value
    console.log(nth(index(), 0))
    
    // the 3rd value
    console.log(nth(index(), 2))
    
    // the 10th value?
    console.log(nth(index(), 9))
    
    // out-of-bounds?
    console.log(nth(foo(), 99))