Questions/issues regarding generators

Andreas Rossberg rossberg at google.com
Thu Mar 14 15:56:16 PDT 2013


On 14 March 2013 23:38, Brendan Eich <brendan at mozilla.com> wrote:
> Andreas Rossberg wrote:
>>
>> That leaves my original proposal not to have generator application
>> return an iterator, but only an iterable. Under that proposal, your
>> example requires disambiguation by inserting the intended call(s) to
>> .iterator in the right place(s).
>
> That's horribly inconvenient. It takes Dmitry's example:
>
>  function* enum(from, to) { for (let i = from; i <= to; ++i) yield i }
>
>  let rangeAsGenerator = enum(1, 4)
>  let dup = zip(rangeAsGenerator, rangeAsGenerator)  // Oops!
>
> which contains a bug under the Harmony proposal, to this:
>
>  function* enum(from, to) { for (let i = from; i <= to; ++i) yield i }
>
>  let rangeAsGenerator = enum(1, 4)
>  let dup = zip(rangeAsGenerator[@iterator](), rangeAsGenerator[@iterator]())

No, why? The zip function invokes the iterator method for you.

See my reply to Jason: I think that in most practical cases (in
particular, all abstractions over iterables), the invocation of the
iterator method will happen inside an abstraction, and the programmer
does not have to worry about it.

> which while it works, is just silly given JS's mutable object heritage.
> Programmers will not write this. They will instead write
>
>  function* enum(from, to) { for (let i = from; i <= to; ++i) yield i }
>
>  let dup = zip(enum(1, 4), enum(1, 4))
>
> which is clearer, shorter, and more truthful and beautiful.

And that is perfectly fine under my proposal. :)

> You seem to think iterables are immutable, or something. 'taint so -- JS is
> not ML! :-P

Not sure what that has to do with anything. 8-}

/Andreas


More information about the es-discuss mailing list