Le 19 août 2014 à 06:47, Kevin Smith <[email protected]> a écrit :
> Background: > > http://esdiscuss.org/topic/next-yo-in-newborn-generators > http://esdiscuss.org/topic/april-8-2014-meeting-notes > > It appears that the current state of affairs is that the argument supplied to > the first call of `next` on a newborn generator is "ignored and inaccessibe". > > Clearly, this means that there are some iterators which cannot be expressed > as a generator (namely, any iterator that echoes back it's `next` arguments). > It seems like there should be parity here. > > More concretely, the fact that information can be passed into generators > means that they can be used to create data sinks. Since that first input is > inaccessible, however, this use case is made more awkward than it needs to > be; the consumer has to artificially "pump" the generator to get past that > first (useless) `next`. > > Is there any way that the generator function can have access to that lost > data? > > Thanks! This can be worked around. Basically, ask the generator to advance to the first `yield` at instantiation, and retrieve the value of the "first" `next()` with that `yield`. For example: ```js // the echo generator, dropping the first .next() function* echo() { var x while (true) { x = yield x } } var iter = echo() iter.next(3) // {value: undefined, done: false} iter.next(8) // {value: 8, done: false} iter.next(1) // {value: 1, done: false} // the same, advancing to the first `yield` at instantiation class echo2 extends echo { construct(...args) { let iter = super(...args) iter.next() return iter } } var iter = echo2() iter.next(3) // {value: 3, done: false} iter.next(8) // {value: 8, done: false} iter.next(1) // {value: 1, done: false} ``` —Claude
_______________________________________________ es-discuss mailing list [email protected] https://mail.mozilla.org/listinfo/es-discuss

