Questions/issues regarding generators

Claus Reinke claus.reinke at talk21.com
Thu Mar 7 09:03:58 PST 2013


>> How about lifting the result, to separate yielded objects and end
>> iteration signalling?
>>
>>    { yields: obj }    // iteration yields obj
>>    {} // iteration ends
> 
> Yes, that would be the proper encoding of an Option/Maybe type, which
> in the abstract is the ideal (the end object might carry a return
> value, though).

So, more of an Either type, which isn't yet easy to match in ES6.

> However, I did not propose that because some around here would
> probably be unhappy about the extra allocation that is required for
> every iteration element under this approach.

One of the reasons for avoiding exceptions is to enable optimizations,
though, and looking through the call, one might be able to avoid the
intermediate allocation for the frequently used path (yield), falling
back to extra allocation only for the iteration end. Or allocate the
wrapper once, then reuse/fill it on each iteration and overwrite it
on iteration end.

Not sure how difficult that (inline and match up construct/deconstruct 
to avoid intermediate allocation) would be for JS engines... but using
exceptions would close most doors for optimization, so it might be
more costly overall?

Claus
 


More information about the es-discuss mailing list