Lucio Tato (2013-10-26T17:01:31.000Z)
domenic at domenicdenicola.com (2013-11-02T19:01:52.783Z)
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? ```js function fibonacci() { let [prev, curr] = [0, 1]; for (;;) { [prev, curr] = [curr, prev + curr]; yield(curr); }} ``` Generators can be iterated over in loops: ```js for (n of new Generator(fibonacci) { // truncate the sequence at 1000 if (n > 1000) break; print(n);} ``` Generators are iterators: ```js 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" ```js 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()); ```