Proposal: for-of-withas a way to provide a value to the generator
Marius Gundersen
gundersen at gmail.com
Tue Aug 22 07:36:13 UTC 2017
One of the reasons I think for-of should handle this is because of the
error handling. [Closing an iterator](
http://raganwald.com/2017/07/22/closing-iterables-is-a-leaky-abstraction.html)
is not trivial, and so having to write that code yourself is error prone.
I originally considered `continue x;` (and `break x;`), but this is already
used for labels, but it seems `continue with x;` (and `break with x;`) will
work, as the following throws a syntax error today: `with:
while(true){continue with;}`. Only problem as you said Isaiah is that you
can't send an initial value, but at least until [`function.sent`](
https://github.com/allenwb/ESideas/blob/master/Generator%20metaproperty.md)
proposal is accepted that is not a problem, as there is no way for the
generator to receive the initial `next` value, so it's not really of much
value today. And since the `Iterarot.return()` method takes an optional
value as well, it makes sense to have support for `break with x;`
On Tue, Aug 22, 2017 at 8:41 AM, Isiah Meadows <isiahmeadows at gmail.com>
wrote:
> 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
>>>
>>
> _______________________________________________
> 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/ae7e7641/attachment.html>
More information about the es-discuss
mailing list