Generator use-cases

Brendan Eich brendan at mozilla.org
Sat Mar 29 07:58:32 PDT 2008


On Mar 29, 2008, at 7:40 AM, Igor Bukanov wrote:

> On 29/03/2008, Kris Zyp <kris at sitepen.com> wrote:
>> I think Neil's inspiring demonstration of pseudo-threading with  
>> generators
>>  is also worthy of inclusion in your list of generator use cases:
>>  http://www.neilmix.com/2007/02/07/threading-in-javascript-17/
>
> That code can be written without generators. In general whenever the
> code in the examples from the blog does yield, one can replace that
> with "return function() { the rest of code from the function}". But
> this would require to replace imperative loops from the examples by
> recursive functions.

This particular continuation-passing style would also require proper  
tail calls

http://wiki.ecmascript.org/doku.php?id=proposals:proper_tail_calls

to be normative, but (news flash) proper tail calls are out of ES4 as  
of yesterday's Ecma TC39 meeting, by general (regretful, in Mozilla's  
case) agreement.

> This shows that if one programs in a functional style, then generators
> are not that useful. But they are valuable if the code uses explicit
> loops etc. Plus with generators one can assume certain time and space
> complexity bounds in ES4 which is not the case for functional code in
> the view of deferred tail call proposal.

I'm glad you mentioned tail calls. I'd word it more strongly, as  
above: tail calls are required if CPS is the preferred style to use  
in lieu of generators. Of course my mail showed a non-CPS expansion  
of generators into functions and objects, which does not require tail  
calls. But it's verbose, sub-optimal for implementations, and error  
prone compared to generators.

/be




More information about the Es4-discuss mailing list