Proposal to simplify Generators impact

Lucio Tato luciotato at
Sat Oct 26 10:01:31 PDT 2013

It's really needed to make js syntax more complex in order to implement
It's function* really needed?
can you just expose "Generator" as a core function?
can "yield" be a function-call-like-construct instead of a new language

function fibonacci() {
    let [prev, curr] = [0, 1];
    for (;;) {
        [prev, curr] = [curr, prev + curr];

Generators can be iterated over in loops:

for (n of new Generator(fibonacci) {
    // truncate the sequence at 1000
    if (n > 1000)

Generators are iterators:

let seq = new Generator(fibonacci);print(; //
1print(; // 2print(; // 3print(; //
5print(; // 8

Advantages: No new syntax, no "function*" (Cognitive dissonance, every good
C programmer can't stop reading function* as "function pointer"). No
"yield" new construction. No added complexity to the language syntax.

By not adding new elements and complexity to the language, you're keeping
it consistent.

By using "new Generator(fn)" to create a Object-Generator, you can also
expose a "done" property and other useful info in a standard way.

Note: why yield syntax should be like a function call:
In actual implementations (V8 --harmony) "yield" will "return" the value
passed in the call to ".next"

function giveNext(){ //generator
  let actual = 10;
  while (actual<100){
       skip = yield(actual);
       actual = actual + skip || 5;
let seq = new Generator(giveNext);print(; //
10print(; // 15print(; // 35print(;
*// 40****let* seq = new Generator(giveNext);
while (!seq.done)
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <>

More information about the es-discuss mailing list