domenic at domenicdenicola.com (2013-07-19T15:39:28.785Z)
Le 16/07/2013 19:54, Claus Reinke a écrit :
> ```js
> // this doesn't work
> function* generator(){
> [1,2,3].forEach( function(x){ yield x } )
> }
> ```
I have been thinking and with for..of, I can't find a good reason to use
.forEach instead of for..of.
for..of does what you need here with generators too.
> For the specific case of forEach et al
What do you mean by "et al"? I don't believe .map, .reduce or .filter
are any interesting to use alongside generators.
Even if so, for..of can work too and is decently elegant (YMMV):
function* g(){
[1,2,3].map(x => {yield transform(x)})
}
becomes
function* g(){
for(x of [1,2,3]) yield transform(x);
}
Le 16/07/2013 19:54, Claus Reinke a écrit : > // this doesn't work > function* generator(){ > [1,2,3].forEach( function(x){ yield x } ) > } I have been thinking and with for..of, I can't find a good reason to use .forEach instead of for..of. for..of does what you need here with generators too. >> This would make generators deep, violating the non-interleaving >> assumptions >> of intermediate callers on the call stack. This is why we accepted >> generators only on condition that they be shallow. We knew at the >> time that >> this privileges built-in control structures over user defined ones. The >> alternative would have been to omit generators completely. We agree that >> shallow generators were worth it, despite this non-uniformity. > > While I understand the compromise, and the wish to get in some form > of generators anyway, the discrimination against user-defined control > structures troubles me deeply. It introduces a new language construct > that defies abstraction. It means that we can no longer use functional > abstraction freely, but have to worry about interactions with generators. > > For the specific case of forEach et al What do you mean by "et al"? I don't believe .map, .reduce or .filter are any interesting to use alongside generators. Even if so, for..of can work too and is decently elegant (YMMV): function* g(){ [1,2,3].map(x => {yield transform(x)}) } becomes function* g(){ for(x of [1,2,3]) yield transform(x); } David