On Tue, Jul 30, 2013 at 2:14 PM, Tab Atkins Jr. <[email protected]> wrote:
> On Tue, Jul 30, 2013 at 2:10 PM, Andrew Fedoniouk
> <[email protected]> wrote:
>> On Tue, Jul 30, 2013 at 1:43 PM, Rick Waldron <[email protected]> wrote:
>>> Or do you mean to say that generator functions and yield should be removed?
>>
>> In principle, with functions-as-collections the yield and the whole
>> generators stuff
>> is not needed. If functions can be used on the right of 'in' or 'of'
>> in 'for' then
>> all 'yield' use cases that I saw so far can be implemented without the yield.
>> So why do we need redundant entities?
>
> You don't understand what yield does.  It freezes the execution of the
> function at that point, waiting until .next() is called to resume
> execution.  This means you can do a number of very convenient things,
> like yielding in the middle of a loop, or yielding within a try/catch
> block.

Trust me, I do understand what yield does. Check the link I've
provided - it implements
in C++ just what you said.


OK, here is an example that has multiple yielding exits:

function fruits() {
  var state = 0;
  return function() {
    switch(state++) {
      case 0: return "apple";
      case 1: return "orange";
      case 2: return "lime";
    }
  }
}

So if you will run this:

for(var fr in fruits)
  console.log( fr )

you will get
 apple
 orange
 lime

printed out.

Close enough to what you will do with yield.

*Any* yield use case can be reproduced this way.

>
> You can't do these with simple functions without significant
> refactoring.  The convenience of yield is well-established by Python.
>

Yep, but don't forget that Python uses reference counting.
In Python generator creation and destruction is deterministic -
you create it at the entry of for loop and it gets destroyed (or at
least can be)
at the end of it.  In JS the only option is to make stack GC-able thing -
generator gets created and live until the GC cycle.

Is this acceptable price for the feature - I really don't know.
But I do know that for simple enumerators/iterators cases
plain functions are more optimal in JS case. And they do not require
such quite ugly syntax changes as "function*".


-- 
Andrew Fedoniouk.

http://terrainformatica.com
_______________________________________________
es-discuss mailing list
[email protected]
https://mail.mozilla.org/listinfo/es-discuss

Reply via email to