Re: Throwing StopIteration in array extras to stop the iteration

2013-03-06 Thread Jason Orendorff
On Tue, Mar 5, 2013 at 6:57 PM, Brendan Eich bren...@mozilla.com wrote: Jason Orendorff wrote: break; // exiting JS loops since 1994 1995! Mercurial logs only go back to 2007, CVS only to 1998, so I will have to take your word for it! :) -j

Re: Throwing StopIteration in array extras to stop the iteration

2013-03-05 Thread David Bruant
Le 05/03/2013 00:31, Jason Orendorff a écrit : On Sun, Mar 3, 2013 at 12:45 PM, David Bruant bruan...@gmail.com mailto:bruan...@gmail.com wrote: [2, 8, 7].forEach(function(e){ if(e === 8) throw StopIteration; This would be taking a piece of one

Re: Throwing StopIteration in array extras to stop the iteration

2013-03-05 Thread Brendan Eich
David Bruant wrote: I'm happy of the outcome of the thread if .findIndex is introduced, but I can't help wondering whether a new method is going to be introduced every single time someone brings up a pattern that would make good use of stopping an interation early. Lacking an iteration

Re: Throwing StopIteration in array extras to stop the iteration

2013-03-05 Thread Jason Orendorff
On Tue, Mar 5, 2013 at 5:42 AM, David Bruant bruan...@gmail.com wrote: Currently, if one wants to do stop an iteration early, he/she has to be done one of the following way: 1) try{ [2, 8, 7].forEach(function(e){ if(e === 8) throw whatever;

Re: Throwing StopIteration in array extras to stop the iteration

2013-03-05 Thread Kevin Gadd
Maybe I'm overlooking some past discussion on this, but if you want to be able to generally terminate iteration in constructs like Array.forEach (this seems like a pretty real world use-case), why not introduce an additional argument to the forEach/etc callbacks, for an 'iteration token'? I.e. if

Re: Throwing StopIteration in array extras to stop the iteration

2013-03-05 Thread David Bruant
Le 05/03/2013 18:32, Jason Orendorff a écrit : On Tue, Mar 5, 2013 at 5:42 AM, David Bruant bruan...@gmail.com mailto:bruan...@gmail.com wrote: Currently, if one wants to do stop an iteration early, he/she has to be done one of the following way: 1) try{ [2, 8,

Re: Throwing StopIteration in array extras to stop the iteration

2013-03-05 Thread Tab Atkins Jr.
On Tue, Mar 5, 2013 at 9:38 AM, Kevin Gadd kevin.g...@gmail.com wrote: Maybe I'm overlooking some past discussion on this, but if you want to be able to generally terminate iteration in constructs like Array.forEach (this seems like a pretty real world use-case), why not introduce an

Re: Throwing StopIteration in array extras to stop the iteration

2013-03-05 Thread Andrea Giammarchi
love the token/Halt idea +1 easy to polyfill too However, and this is for David, some and every are *not* the opposite. With some, you return true only when you want to exit, with every you have to return true or false accordingly. So, the return true in Array#some() is the equivalent of a

Re: Throwing StopIteration in array extras to stop the iteration

2013-03-05 Thread David Bruant
Le 05/03/2013 17:37, Brendan Eich a écrit : David Bruant wrote: I'm happy of the outcome of the thread if .findIndex is introduced, but I can't help wondering whether a new method is going to be introduced every single time someone brings up a pattern that would make good use of stopping an

Re: Throwing StopIteration in array extras to stop the iteration

2013-03-05 Thread Brendan Eich
Jason Orendorff wrote: break; // exiting JS loops since 1994 1995! /be ___ es-discuss mailing list es-discuss@mozilla.org https://mail.mozilla.org/listinfo/es-discuss

Re: Throwing StopIteration in array extras to stop the iteration

2013-03-04 Thread Jeff Walden
On 03/03/2013 06:53 PM, Andrea Giammarchi wrote: I had to check msdn rather than MDN since latter does not mention it while mans shows an example: http://msdn.microsoft.com/en-us/library/ie/3k9c4a32(v=vs.94).aspx The RegExp statics aren't mentioned because they're a bad idea, imposing costs

Re: Throwing StopIteration in array extras to stop the iteration

2013-03-04 Thread Jeff Walden
On 03/03/2013 06:49 PM, Rick Waldron wrote: Is this +1 to findIndex? Not that I much care between the two, just making sure another reasonable name is considered, but I'm not sure why it wouldn't be named find rather than findIndex. The index seems like the only bit you'd reasonably be

Re: Throwing StopIteration in array extras to stop the iteration

2013-03-04 Thread Andrea Giammarchi
I use RegExp.$1 , RegExp.$2, etc quite a lot since I am a huge fan of the RAM and I believe creating a redundant array of matches for no reason since these are retrievable in any case through the RegExp constructor, considering exec and match points to those RegExp properties anyhow, ain't needed

Re: Throwing StopIteration in array extras to stop the iteration

2013-03-04 Thread Andrea Giammarchi
that does not find a thing, that find an index I meant that simply store once the index without needing an outer scope access and variable inside the closure On Mon, Mar 4, 2013 at 8:38 AM, Andrea Giammarchi andrea.giammar...@gmail.com wrote: I use RegExp.$1 , RegExp.$2, etc quite a lot

Re: Throwing StopIteration in array extras to stop the iteration

2013-03-04 Thread Bjoern Hoehrmann
* Jeff Walden wrote: On 03/03/2013 06:49 PM, Rick Waldron wrote: Is this +1 to findIndex? Not that I much care between the two, just making sure another reasonable name is considered, but I'm not sure why it wouldn't be named find rather than findIndex. The index seems like the only bit

Re: Throwing StopIteration in array extras to stop the iteration

2013-03-04 Thread Rick Waldron
On Mon, Mar 4, 2013 at 11:29 AM, Jeff Walden jwalden...@mit.edu wrote: On 03/03/2013 06:49 PM, Rick Waldron wrote: Is this +1 to findIndex? Not that I much care between the two, just making sure another reasonable name is considered, but I'm not sure why it wouldn't be named find rather

Re: Throwing StopIteration in array extras to stop the iteration

2013-03-04 Thread Andrea Giammarchi
this is what 've wrote as prototype at the end of the post. findIndex makes sense to me and is better, in term of manipulation, than finding just an element. (function(AP){ AP.findIndex || ( AP.findIndex = function(fn, self) { var $i = -1; AP.some.call(this, function(v, i, a) { if

Re: Throwing StopIteration in array extras to stop the iteration

2013-03-04 Thread Rick Waldron
On Mon, Mar 4, 2013 at 2:37 PM, Andrea Giammarchi andrea.giammar...@gmail.com wrote: this is what 've wrote as prototype at the end of the post. findIndex makes sense to me and is better, in term of manipulation, than finding just an element. (function(AP){ AP.findIndex || (

RE: Throwing StopIteration in array extras to stop the iteration

2013-03-04 Thread Domenic Denicola
From: es-discuss-boun...@mozilla.org [es-discuss-boun...@mozilla.org] on behalf of Rick Waldron [waldron.r...@gmail.com] Thanks, I've submitted an agenda item that includes _both_ find and findIndex. Awesome! One issue with `find` is what distinguishes find([1, 2, 3], x = isNaN(x)); //

Re: Throwing StopIteration in array extras to stop the iteration

2013-03-04 Thread Brendan Eich
One idea we've discussed: allow the sentinel that is OOB with respect to the domain (element type) be an optional second parameter: const K = Symbol(not found); console.log( [1, 2, 3].find(x = isNaN(x), K) ); // K, logs as not found console.log( [1, 2, , 3].find(x = isNaN(x)) ); //

Re: Throwing StopIteration in array extras to stop the iteration

2013-03-04 Thread Andrea Giammarchi
Thanks a lot. On find(), I believe it will always be ambiguous, compared to findIndex, for the simple reason that an Array could contain undefined too [1, undefined, 2] ... probably an edge case not worth the consideration, but this looks like a legit code to me: [1, undefined, 2].find(function

Re: Throwing StopIteration in array extras to stop the iteration

2013-03-04 Thread Jeff Walden
On 03/04/2013 08:38 AM, Andrea Giammarchi wrote: I believe creating a redundant array of matches for no reason since these are retrievable in any case through the RegExp constructor, considering exec and match points to those RegExp properties anyhow, ain't needed when re.test(value) is

Re: Throwing StopIteration in array extras to stop the iteration

2013-03-04 Thread Jason Orendorff
On Sun, Mar 3, 2013 at 12:45 PM, David Bruant bruan...@gmail.com wrote: [2, 8, 7].forEach(function(e){ if(e === 8) throw StopIteration; This would be taking a piece of one low-level protocol, and using it for a sorta-kinda related thing that actually, on

Re: Throwing StopIteration in array extras to stop the iteration

2013-03-04 Thread Andrea Giammarchi
I didn't forget in the post, neither is necessary here, I put in the post to deal with an integer but that's superflous for the purpose so no mistake (we all know arr[1] is gonna be handled as arr[1], as example) Anyway, RegExp.$N is just fine and standard across all engines I know and

Re: Throwing StopIteration in array extras to stop the iteration

2013-03-03 Thread Bjoern Hoehrmann
* David Bruant wrote: One (minor) annoyance with forEach/map, etc. is that the enumeration can't be stopped until all elements have been traversed which doesn't suit every use case. One hack to stop the enumeration is to throw an error but that requires to wrap the .forEach call in a try/catch

Re: Throwing StopIteration in array extras to stop the iteration

2013-03-03 Thread David Bruant
Le 03/03/2013 19:56, Bjoern Hoehrmann a écrit : * David Bruant wrote: One (minor) annoyance with forEach/map, etc. is that the enumeration can't be stopped until all elements have been traversed which doesn't suit every use case. One hack to stop the enumeration is to throw an error but that

Re: Throwing StopIteration in array extras to stop the iteration

2013-03-03 Thread Brendan Eich
David Bruant wrote: Le 03/03/2013 19:56, Bjoern Hoehrmann a écrit : * David Bruant wrote: One (minor) annoyance with forEach/map, etc. is that the enumeration can't be stopped until all elements have been traversed which doesn't suit every use case. One hack to stop the enumeration is to throw

Re: Throwing StopIteration in array extras to stop the iteration

2013-03-03 Thread David Bruant
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:

Re: Throwing StopIteration in array extras to stop the iteration

2013-03-03 Thread Brendan Eich
(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:

Re: Throwing StopIteration in array extras to stop the iteration

2013-03-03 Thread Oliver Hunt
On Mar 3, 2013, at 12:05 PM, Brendan Eich bren...@mozilla.com wrote: (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 the naming boat has sailed, and i'm

Re: Throwing StopIteration in array extras to stop the iteration

2013-03-03 Thread Bjoern Hoehrmann
* 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;

Re: Throwing StopIteration in array extras to stop the iteration

2013-03-03 Thread Rick Waldron
On Sun, Mar 3, 2013 at 3:34 PM, Bjoern Hoehrmann derhoe...@gmx.net wrote: * 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;

Re: Throwing StopIteration in array extras to stop the iteration

2013-03-03 Thread Brendan Eich
Bjoern Hoehrmann wrote: * 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)){

Re: Throwing StopIteration in array extras to stop the iteration

2013-03-03 Thread Andrea Giammarchi
hacky but easy to solve reusing once a RegExp var i = [1,2,3].some(function(v, i){ return v === 2 this.test(i); }, /\d+/) RegExp['$']; alert(i); // 1 no extra loop, no global anything, just an extra object needed, the RegExp, actually something you could create once in your closure and

Re: Throwing StopIteration in array extras to stop the iteration

2013-03-03 Thread Andrea Giammarchi
and post with few variants: http://webreflection.blogspot.com/2013/03/breaking-array-extras.html br On Sun, Mar 3, 2013 at 3:15 PM, Andrea Giammarchi andrea.giammar...@gmail.com wrote: hacky but easy to solve reusing once a RegExp var i = [1,2,3].some(function(v, i){ return v === 2

Re: Throwing StopIteration in array extras to stop the iteration

2013-03-03 Thread Brendan Eich
Andrea Giammarchi wrote: hacky but easy to solve reusing once a RegExp var i = [1,2,3].some(function(v, i){ return v === 2 this.test(i); }, /\d+/) RegExp['$']; My Perl 4 _homage_, blechh. Thanks, this will help sell findIndex to TC39 :-P. /be alert(i); // 1 no extra loop, no global

Re: Throwing StopIteration in array extras to stop the iteration

2013-03-03 Thread Brendan Eich
Brendan Eich wrote: Andrea Giammarchi wrote: RegExp['$']; My Perl 4 _homage_, blechh. (You could at least have had the decency to use RegExp.lastMatch... ;-) /be ___ es-discuss mailing list es-discuss@mozilla.org

Re: Throwing StopIteration in array extras to stop the iteration

2013-03-03 Thread Rick Waldron
On Sun, Mar 3, 2013 at 5:33 PM, Brendan Eich bren...@mozilla.com wrote: Bjoern Hoehrmann wrote: * 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

Re: Throwing StopIteration in array extras to stop the iteration

2013-03-03 Thread Brandon Benvie
I was going to say... indexOf with a callback is the best thing since sliced bread. On Mar 3, 2013, at 5:26 PM, Rick Waldron waldron.r...@gmail.com wrote: On Sun, Mar 3, 2013 at 5:33 PM, Brendan Eich bren...@mozilla.com wrote: Bjoern Hoehrmann wrote: * David Bruant wrote: I've found

Re: Throwing StopIteration in array extras to stop the iteration

2013-03-03 Thread Rick Waldron
On Sun, Mar 3, 2013 at 9:38 PM, Brandon Benvie bben...@mozilla.com wrote: I was going to say... indexOf with a callback is the best thing since sliced bread. Is this +1 to findIndex? I ask because: var a = function() {}, array = [a]; array.indexOf(a); // 0 Rick On Mar 3, 2013, at

Re: Throwing StopIteration in array extras to stop the iteration

2013-03-03 Thread Andrea Giammarchi
well, lastMatch is 9 chars against ['$'] 6 but actually didn't know about lastMatch, it looks like to have more info about those properties, included lastParen, I had to check msdn rather than MDN since latter does not mention it while mans shows an example:

Re: Throwing StopIteration in array extras to stop the iteration

2013-03-03 Thread Andrea Giammarchi
had instantly same thought ... I use functions in Array very often to handle/manage asynchronous queues br On Sun, Mar 3, 2013 at 6:49 PM, Rick Waldron waldron.r...@gmail.com wrote: On Sun, Mar 3, 2013 at 9:38 PM, Brandon Benvie bben...@mozilla.comwrote: I was going to say...