Proposal to simplify Generators impact

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


It's really needed to make js syntax more complex in order to implement
generators?
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
construction?

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

Generators can be iterated over in loops:

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

Generators are iterators:

let seq = new Generator(fibonacci);print(seq.next()); //
1print(seq.next()); // 2print(seq.next()); // 3print(seq.next()); //
5print(seq.next()); // 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(seq.next()); //
10print(seq.next()); // 15print(seq.next(20)); // 35print(seq.next());
*// 40****let* seq = new Generator(giveNext);
while (!seq.done)
   print(seq.next());
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20131026/1ae5f849/attachment.html>


More information about the es-discuss mailing list