Why can’t for-of be applied to iterators?

Brendan Eich brendan at mozilla.com
Mon Jun 10 17:36:48 PDT 2013


Brendan Eich wrote:
> Erik Arvidsson wrote:
>> We really need to be able to do for-of with arrays and node lists 
>> etc, without having to call .values() on it. It is such a common 
>> operation that we should not tax this further.
>
> Agreed. Consider Peter Norvig's Sudoku solver, which I ported to JS1.7 
> years ago, ported to ES6:
>
> https://gist.github.com/anonymous/5753636

Better version at

https://gist.github.com/BrendanEich/5753666

(Stripped __iterator__ left-overs from JS1.7 era -- hooray for iterable 
Arrays in ES6!)

/be

>
> This code is nearly as concise as the Python original. Requiring 
> values() calls all over does no one any favors.
>
> The idea that iterators return themselves from their @@iterator 
> (__iter__) hook has years of actual programmer mileage in Python. I 
> take that experience seriously, compared to arguments from purity that 
> tax the common use-cases.
>
> /be
>
>>
>>
>> On Mon, Jun 10, 2013 at 3:32 AM, Andy Wingo <wingo at igalia.com 
>> <mailto:wingo at igalia.com>> wrote:
>>
>>     On Sun 09 Jun 2013 11:34, Jason Orendorff
>> <jason.orendorff at gmail.com <mailto:jason.orendorff at gmail.com>> writes:
>>
>> > I think it's a mistake for iterators not to be iterable.
>>
>>     I agree, FWIW.
>>
>>     I think I would go farther and suggest that _only_ iterators be
>>     iterable.  That way, the RHS of a for-of is expected to be an
>>     iterator.
>>     In the worst case you end up having:
>>
>>       var a = [1, 2, 3, 4]
>>       for (let x of values(a))
>>         ...
>>
>>     instead of
>>
>>       for (let x of a)
>>         ...
>>
>>     which to my eye is better anyway.  Making an "itertools"-like 
>> library
>>     difficult is a definite drawback of the current spec.
>>
>>     Andy
>>     _______________________________________________
>>     es-discuss mailing list
>>     es-discuss at mozilla.org <mailto:es-discuss at mozilla.org>
>>     https://mail.mozilla.org/listinfo/es-discuss
>>
>>
>>
>>
>> -- 
>> erik
>> _______________________________________________
>> 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
>


More information about the es-discuss mailing list