Throwing StopIteration in array extras to stop the iteration

Bjoern Hoehrmann derhoermi at gmx.net
Sun Mar 3 12:34:29 PST 2013


* David Bruant wrote:
>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's usually a bad idea to trigger side-effects from such callbacks. The
proper solution here would be having a suitable method available, like

  var index = array.findIndex(someCondition);

as it is called in Haskell and C#.
-- 
Björn Höhrmann · mailto:bjoern at hoehrmann.de · http://bjoern.hoehrmann.de
Am Badedeich 7 · Telefon: +49(0)160/4415681 · http://www.bjoernsworld.de
25899 Dagebüll · PGP Pub. KeyID: 0xA4357E78 · http://www.websitedev.de/ 


More information about the es-discuss mailing list