Throwing StopIteration in array extras to stop the iteration

Brendan Eich brendan at mozilla.com
Sun Mar 3 12:05:30 PST 2013


(a) not a compatible change for engines already prototyping StopIteration.
(b) wrong hammer for this nail.

Are the some and every names confusing? Would any and all be better?

I think this is an RTFM thing, and people are smart, they memorize what 
they need to know.

/be

David Bruant wrote:
> Le 03/03/2013 20:29, Brendan Eich a écrit :
>> If you want some or every and not forEach, they are there -- use 
>> them. No exception required.
> I've found myself multiple times in a situation where I needed the 
> index of the first element responding to some conditions. I solved it 
> the following way:
>
>     var index;
>     array.some(function(e, i){
>         if(someCondition(e)){
>             index = i;
>             return false;
>         }
>
>         return true;
>     })
>
> It works, but felt a bit awkward. It's a hack on .some because there 
> is no other way to stop an iteration in other array methods.
> Also spending hours on debugging because someone confused "some" for 
> "every" (mixed the meaning of true/false) isn't fun.
>
>     var index;
>     array.forEach(function(e, i){
>         if(someCondition(e)){
>             index = i;
>             throw StopIteration;
>         }
>     })
>
> would look more explicit in my opinion.
>
> David
>


More information about the es-discuss mailing list