36

ECMAScript 6 should be bringing generator functions and iterators. A generator function (which has the function* syntax) returns an iterator. The iterator has a next method which, when repeatedly called, executes the body of the generator function, repeatedly pausing and resuming execution at every yield operator.

The ECMAScript 6 wiki on generators also introduces the "delegated yield" yield* operator as follows:

The yield* operator delegates to another generator. This provides a convenient mechanism for composing generators.

What does "delegate to another generator" mean? How can I use yield* to "conveniently compose generators"?

[You can play with generators in Node v0.11.3 with the --harmony-generators flag.]

Felix Kling
  • 705,106
  • 160
  • 1,004
  • 1,072
Randomblue
  • 98,379
  • 133
  • 328
  • 526
  • 1
    The [wiki](http://wiki.ecmascript.org/doku.php?id=harmony:generators#delegating_yield) says, *"This is similar to a for-in loop over the generator,"*, so `yield* generator` should be similar to `for (i of generator) { yield i; }`. – Felix Kling Jul 05 '13 at 15:07
  • 2
    if you know python, this is what [yield from](http://docs.python.org/dev/whatsnew/3.3.html#pep-380) does. – georg Jul 05 '13 at 15:19
  • Yield bomb? `(function*_(){yield*(_())})().next()` – user157251 Jan 22 '14 at 19:34

3 Answers3

50

Delegating to another generator means the current generator stops producing values by itself, instead yielding the values produced by another generator until it exhausts it. It then resumes producing its own values, if any.

For instance, if secondGenerator() produces numbers from 10 to 15, and firstGenerator() produces numbers from 1 to 5 but delegates to secondGenerator() after producing 2, then the values produced by firstGenerator() will be:

1, 2, 10, 11, 12, 13, 14, 15, 3, 4, 5

function* firstGenerator() {
    yield 1;
    yield 2;
    // Delegate to second generator
    yield* secondGenerator();
    yield 3;
    yield 4;
    yield 5;
}

function* secondGenerator() {
    yield 10;
    yield 11;
    yield 12;
    yield 13;
    yield 14;
    yield 15;
}

console.log(Array.from(firstGenerator()));
Retsam
  • 20,299
  • 9
  • 62
  • 82
Frédéric Hamidi
  • 240,249
  • 39
  • 455
  • 462
  • 9
    Nice. It's obvious in hindsight, but I find specs hart to read. One improvement would be to add the example code that produces the result you mention. – qubyte Feb 09 '14 at 17:10
2
function *gimme1to2_10to15_3to5() {
    var ten = gimme10to15();
    yield 1; yield 2;
    for (var i = 10; i <= 20; i++)
        yield *ten;
    yield 3; yield 4; yield 5;
    }

function *gimme10to15() {
    for (var i = 10; i <= 15; i++)
        var x = yield i;
    }

let gen = gimme1to2_10to15_3to5();

var ar = [];
for (var i = 0; i < 12; i++)
    {
    var r = gen.next();
    ar [i] = r.value + (r.done ? "!" : "..");
    }
console.log (ar.join (", "));

The result is

1.., 2.., 10.., 11.., 12.., 13.., 14.., 15.., 3.., 4.., 5.., undefined!

Notes

  1. The function with yield * asks for 11 values from the inner generator but only receives the 10..15 that it should. The excess yield * calls have no effect.

  2. The done value of the inner generator has no effect on the done that its caller returns.

  3. The output shows undefined! at the end because the testing loop is written to go one beyond the required number of values in order to show the generator's done = true

  4. This output is from the ES6 testing ground, Babel, and the semantics exhibited above should be considered provisional as at 5th Feb 2016.

2

Delegated yield doesn't have to delegate to another generator only, but to any iterator, so the first answer is a bit inconclusive. Consider this simple example:

`
function *someGenerator() {
    yield 0;
    yield [1,2,3]
    yield* [1,2,3] 
}

for (v of someGenerator()) {
    console.log(v);
}

`

There isn't another function inside the generator itself - yet yield delegates to the iterator of an Array Object Iterator ;-)

afkplus
  • 115
  • 2
  • 11
Inoperable
  • 1,339
  • 5
  • 15
  • 30