On Tue, Jul 30, 2013 at 1:43 PM, Rick Waldron <[email protected]> wrote: > > > > On Tue, Jul 30, 2013 at 3:39 PM, Andrew Fedoniouk > <[email protected]> wrote: >> >> Slightly different approach that I've found [1] quite useful. >> >> Consider this simple function: >> >> function range(start,end) { >> var i = start; >> return function() { >> if( i < end ) return i++; >> } >> } >> >> It returns so called iterator function (not an Iterator but just a >> function). >> >> and imagine that for/in ( or for/of ) >> >> for(var el in something) >> >> statement is modified to support functions as collections. So body of >> the for loop is called until function >> `something` returns anything but not void. >> >> >> Having this this we can write something like this: >> >> for(var i in range(12,48)) { >> console.log(i); >> } >> >> In principle this simple change eliminates need of most of >> generator/yield cases. > > > > Which is exactly this: > > function * range(start, end) { > for (var i = start; i <= end; i++) { > yield i; > } > } > > for (var value of range(0, 9)) { > console.log( value ); > } >
Yes, it is close conceptually. With only exception: yield requires stack itself to be heap allocated thing. Many things around this. In C++ I did very lightweight $generator/$yield implementation [1] but unfortunately that trick is not available in JS. > > 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? [1] http://www.codeproject.com/Articles/29524/Generators-in-C -- Andrew Fedoniouk. http://terrainformatica.com _______________________________________________ es-discuss mailing list [email protected] https://mail.mozilla.org/listinfo/es-discuss

