Proposal: for-of-withas a way to provide a value to the generator
Isiah Meadows
isiahmeadows at gmail.com
Tue Aug 22 06:41:44 UTC 2017
Edit: remove bad email
On Tue, Aug 22, 2017, 02:40 Isiah Meadows <isiahmeadows at gmail.com> wrote:
> To be honest, I'm not too sold on either of these:
>
> 1. The `with <expr>` reads like an expression evaluated *only once*.
>
> 2. The `continue with <expr>` does not provide a way to send a value on
> the first `next` call.
>
> One idea is to use a glorified reducer, like this (simplified):
> ```js
> function pipe(gen, start, func) {
> var iter = gen[Symbol.iterator]()
> var current = iter.next(start)
> while (!current.done) {
> start = func(current.value)
> current = iter.next(start)
> }
> return current.value
> }
> ```
>
> But even that has its limits (inability to recover from errors, for
> example), and it's a pretty inelegant solution to this IMHO.
>
> In general, if you find yourself using iterators like that, `for ... of`
> is quite possibly the most way to use them. Think of them as sync
> send/receive channels, where you send the argument, block during
> processing, and receive through the return value. Here's how you should be
> using them, if you must:
>
> ```js
> var fibs = fibonacci()
> var result = fibs.next(0)
>
> while (!result.done) {
> console.log(value)
> result = fibs.next(8)
> }
> ```
>
> On Mon, Aug 21, 2017, 22:04 jong chan moe <jong at chan.moe> wrote:
>
>> I think it would be natural to use it with the `continue` statement.
>>
>> ```js
>> for (let value of fibonacci()) {
>> console.log(value);
>> continue with value == 8;
>> }
>> ```
>>
>>
>> _______________________________________________
>> es-discuss mailing list
>> es-discuss at mozilla.org
>> https://mail.mozilla.org/listinfo/es-discuss
>>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20170822/02c08f55/attachment-0001.html>
More information about the es-discuss
mailing list