Mark S. Miller (2015-06-24T22:17:49.000Z)
d at domenic.me (2015-07-07T02:07:22.330Z)
Something strange and bizarre that Jafar and I just discovered after the TC39 meeting: We all know that there are a set of conventional combinators, like flatMap, that work fine for iterators. Iterators are uni-directional. Generators are bi-directional. (As Jara observes: Generator implements Iterator, Observer) We thought that these combinators did not apply to generators in their full bidirectional glory. We were wrong. Flatmap of generators can approximately be modeled in terms of "`yield*` wrap" as follows: ```js flatMap([g1, g2]) ``` is equivalent to ```js (function*() { yield* wrap(g1, function.sent); // result of g1 ignored! return yield* wrap(g2, function.sent); }()) ``` On Wed, Jun 24, 2015 at 3:04 PM, Jason Orendorff <jason.orendorff at gmail.com> wrote: > Thanks for finding this discussion. This is exactly what I'm > interested in. What does "wrap" look like? It seems like it would be > pretty involved. Just showing the next method: ```js function wrap(g, prime) { let first = true; return { next(val) { if (first) { first = false; return g.next(prime); } else { return g.next(val); } } //... }; } ``` ALL CODE I WROTE ABOVE IS UNTESTED. Does this make sense? If anyone tests it, does it work?