Array method ranges
Good idea. However, I don't like the fact "arr.forEach(f,null,-1,0)" doesn't walk the array backwards properly. Not sure it's worth to have it built-in though.
Good idea. However, I don't like the fact "arr.forEach(f,null,-1,0)" doesn't walk the array backwards properly. Not sure it's worth to have it built-in though. ---------------------------------------- > Date: Thu, 24 Jan 2013 12:06:23 +0100 > Subject: Array method ranges > From: ecma at qfox.nl > To: es-discuss at mozilla.org > > What about adding specific range arguments to the es5 array methods > (forEach, map, etc)? Currently the start (inclusive) and stop > (exclusive) is always 0 ... length, but what if you only want to map > over a sub range of the array? Or maybe I want to traverse the array > in reverse? I'd either have to slice it or .reverse it, neither are > something I would want. So I fall back to `for` or `while` loops. > > As for the context parameter, I believe undefined won't change the > context opposed to omitting it, right? > > arr.forEach(function(){ ...}); > // same as > arr.forEach(function(){ ...}, undefined, 0, arr.length); > > arr.slice(10,10).forEach... > arr.slice(80,20).reverse().forEach... > => > arr.forEach(function(){ ...}, undefined, 10, 20); > arr.forEach(function(){ ...}, undefined, 100, 80); // run from 100 to > 80, backwards > > Negative numbers could behave the same as in slice (offsets from the > last item, rather than the first). > > arr.forEach(function(){ ...}, undefined, -20); // run from length-20 to length > arr.forEach(function(){ ...}, undefined, -20, -10); // run from > length-20 to length-10 (so, forward) > arr.forEach(function(){ ...}, undefined, -20, -30); // run from > length-20 to length-30 (so, backwards) > > Of course, it would still skip the holes in sparse arrays. > > - peter > _______________________________________________ > es-discuss mailing list > es-discuss at mozilla.org > https://mail.mozilla.org/listinfo/es-discuss
At this point I think we are better of moving towards iterator methods. For example if we had an islice like the one in Python's itertools [*] we can do:
for (let v of islice(arr, start, stop)) { ... }
this would be equivalent to your proposed
arr.forEach((v) => { ... }, undefined, start, stop)
with the benefit that it composes much better.
[*] docs.python.org/2/library/itertools.html#itertools.islice
At this point I think we are better of moving towards iterator methods. For example if we had an islice like the one in Python's itertools [*] we can do: for (let v of islice(arr, start, stop)) { ... } this would be equivalent to your proposed arr.forEach((v) => { ... }, undefined, start, stop) with the benefit that it composes much better. [*] http://docs.python.org/2/library/itertools.html#itertools.islice On Thu, Jan 24, 2013 at 9:45 AM, François REMY <francois.remy.dev at outlook.com> wrote: > Good idea. However, I don't like the fact "arr.forEach(f,null,-1,0)" doesn't walk the array backwards properly. Not sure it's worth to have it built-in though. > > ---------------------------------------- >> Date: Thu, 24 Jan 2013 12:06:23 +0100 >> Subject: Array method ranges >> From: ecma at qfox.nl >> To: es-discuss at mozilla.org >> >> What about adding specific range arguments to the es5 array methods >> (forEach, map, etc)? Currently the start (inclusive) and stop >> (exclusive) is always 0 ... length, but what if you only want to map >> over a sub range of the array? Or maybe I want to traverse the array >> in reverse? I'd either have to slice it or .reverse it, neither are >> something I would want. So I fall back to `for` or `while` loops. >> >> As for the context parameter, I believe undefined won't change the >> context opposed to omitting it, right? >> >> arr.forEach(function(){ ...}); >> // same as >> arr.forEach(function(){ ...}, undefined, 0, arr.length); >> >> arr.slice(10,10).forEach... >> arr.slice(80,20).reverse().forEach... >> => >> arr.forEach(function(){ ...}, undefined, 10, 20); >> arr.forEach(function(){ ...}, undefined, 100, 80); // run from 100 to >> 80, backwards >> >> Negative numbers could behave the same as in slice (offsets from the >> last item, rather than the first). >> >> arr.forEach(function(){ ...}, undefined, -20); // run from length-20 to length >> arr.forEach(function(){ ...}, undefined, -20, -10); // run from >> length-20 to length-10 (so, forward) >> arr.forEach(function(){ ...}, undefined, -20, -30); // run from >> length-20 to length-30 (so, backwards) >> >> Of course, it would still skip the holes in sparse arrays. >> >> - peter >> _______________________________________________ >> 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 -- erik
Yes, this was the plan. I don't see a strawman, yet. Cc'ing jorendorff.
Yes, this was the plan. I don't see a strawman, yet. Cc'ing jorendorff. /be Erik Arvidsson wrote: > At this point I think we are better of moving towards iterator > methods. For example if we had an islice like the one in Python's > itertools [*] we can do: > > for (let v of islice(arr, start, stop)) { > ... > } > > this would be equivalent to your proposed > > arr.forEach((v) => { ... }, undefined, start, stop) > > with the benefit that it composes much better. > > [*] http://docs.python.org/2/library/itertools.html#itertools.islice > > On Thu, Jan 24, 2013 at 9:45 AM, François REMY > <francois.remy.dev at outlook.com> wrote: >> Good idea. However, I don't like the fact "arr.forEach(f,null,-1,0)" doesn't walk the array backwards properly. Not sure it's worth to have it built-in though. >> >> ---------------------------------------- >>> Date: Thu, 24 Jan 2013 12:06:23 +0100 >>> Subject: Array method ranges >>> From: ecma at qfox.nl >>> To: es-discuss at mozilla.org >>> >>> What about adding specific range arguments to the es5 array methods >>> (forEach, map, etc)? Currently the start (inclusive) and stop >>> (exclusive) is always 0 ... length, but what if you only want to map >>> over a sub range of the array? Or maybe I want to traverse the array >>> in reverse? I'd either have to slice it or .reverse it, neither are >>> something I would want. So I fall back to `for` or `while` loops. >>> >>> As for the context parameter, I believe undefined won't change the >>> context opposed to omitting it, right? >>> >>> arr.forEach(function(){ ...}); >>> // same as >>> arr.forEach(function(){ ...}, undefined, 0, arr.length); >>> >>> arr.slice(10,10).forEach... >>> arr.slice(80,20).reverse().forEach... >>> => >>> arr.forEach(function(){ ...}, undefined, 10, 20); >>> arr.forEach(function(){ ...}, undefined, 100, 80); // run from 100 to >>> 80, backwards >>> >>> Negative numbers could behave the same as in slice (offsets from the >>> last item, rather than the first). >>> >>> arr.forEach(function(){ ...}, undefined, -20); // run from length-20 to length >>> arr.forEach(function(){ ...}, undefined, -20, -10); // run from >>> length-20 to length-10 (so, forward) >>> arr.forEach(function(){ ...}, undefined, -20, -30); // run from >>> length-20 to length-30 (so, backwards) >>> >>> Of course, it would still skip the holes in sparse arrays. >>> >>> - peter >>> _______________________________________________ >>> 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 > > >
It looks like the beginnings of an outline were added to the standard modules list living under '@iter' with zip and unzip so far: harmony:modules_standard. But no separate strawman yet.
It looks like the beginnings of an outline were added to the standard modules list living under '@iter' with zip and unzip so far: http://wiki.ecmascript.org/doku.php?id=harmony:modules_standard. But no separate strawman yet. On Thursday, January 24, 2013, Brendan Eich wrote: > Yes, this was the plan. I don't see a strawman, yet. Cc'ing jorendorff. > > /be > > Erik Arvidsson wrote: > >> At this point I think we are better of moving towards iterator >> methods. For example if we had an islice like the one in Python's >> itertools [*] we can do: >> >> for (let v of islice(arr, start, stop)) { >> ... >> } >> >> this would be equivalent to your proposed >> >> arr.forEach((v) => { ... }, undefined, start, stop) >> >> with the benefit that it composes much better. >> >> [*] http://docs.python.org/2/**library/itertools.html#**itertools.islice<http://docs.python.org/2/library/itertools.html#itertools.islice> >> >> On Thu, Jan 24, 2013 at 9:45 AM, François REMY >> <francois.remy.dev at outlook.com> wrote: >> >>> Good idea. However, I don't like the fact "arr.forEach(f,null,-1,0)" >>> doesn't walk the array backwards properly. Not sure it's worth to have it >>> built-in though. >>> >>> ------------------------------**---------- >>> >>>> Date: Thu, 24 Jan 2013 12:06:23 +0100 >>>> Subject: Array method ranges >>>> From: ecma at qfox.nl >>>> To: es-discuss at mozilla.org >>>> >>>> What about adding specific range arguments to the es5 array methods >>>> (forEach, map, etc)? Currently the start (inclusive) and stop >>>> (exclusive) is always 0 ... length, but what if you only want to map >>>> over a sub range of the array? Or maybe I want to traverse the array >>>> in reverse? I'd either have to slice it or .reverse it, neither are >>>> something I would want. So I fall back to `for` or `while` loops. >>>> >>>> As for the context parameter, I believe undefined won't change the >>>> context opposed to omitting it, right? >>>> >>>> arr.forEach(function(){ ...}); >>>> // same as >>>> arr.forEach(function(){ ...}, undefined, 0, arr.length); >>>> >>>> arr.slice(10,10).forEach... >>>> arr.slice(80,20).reverse().**forEach... >>>> => >>>> arr.forEach(function(){ ...}, undefined, 10, 20); >>>> arr.forEach(function(){ ...}, undefined, 100, 80); // run from 100 to >>>> 80, backwards >>>> >>>> Negative numbers could behave the same as in slice (offsets from the >>>> last item, rather than the first). >>>> >>>> arr.forEach(function(){ ...}, undefined, -20); // run from length-20 to >>>> length >>>> arr.forEach(function(){ ...}, undefined, -20, -10); // run from >>>> length-20 to length-10 (so, forward) >>>> arr.forEach(function(){ ...}, undefined, -20, -30); // run from >>>> length-20 to length-30 (so, backwards) >>>> >>>> Of course, it would still skip the holes in sparse arrays. >>>> >>>> - peter >>>> ______________________________**_________________ >>>> es-discuss mailing list >>>> es-discuss at mozilla.org >>>> https://mail.mozilla.org/**listinfo/es-discuss<https://mail.mozilla.org/listinfo/es-discuss> >>>> >>> ______________________________**_________________ >>> es-discuss mailing list >>> es-discuss at mozilla.org >>> https://mail.mozilla.org/**listinfo/es-discuss<https://mail.mozilla.org/listinfo/es-discuss> >>> >> >> >> >> ______________________________**_________________ > es-discuss mailing list > es-discuss at mozilla.org > https://mail.mozilla.org/**listinfo/es-discuss<https://mail.mozilla.org/listinfo/es-discuss> > -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20130124/d9a0e190/attachment.html>
What about adding specific range arguments to the es5 array methods (forEach, map, etc)? Currently the start (inclusive) and stop (exclusive) is always 0 ... length, but what if you only want to map over a sub range of the array? Or maybe I want to traverse the array in reverse? I'd either have to slice it or .reverse it, neither are something I would want. So I fall back to
for
orwhile
loops.As for the context parameter, I believe undefined won't change the context opposed to omitting it, right?
arr.forEach(function(){ ...}); // same as arr.forEach(function(){ ...}, undefined, 0, arr.length);
arr.slice(10,10).forEach... arr.slice(80,20).reverse().forEach... =>
arr.forEach(function(){ ...}, undefined, 10, 20); arr.forEach(function(){ ...}, undefined, 100, 80); // run from 100 to 80, backwards
Negative numbers could behave the same as in slice (offsets from the last item, rather than the first).
arr.forEach(function(){ ...}, undefined, -20); // run from length-20 to length arr.forEach(function(){ ...}, undefined, -20, -10); // run from length-20 to length-10 (so, forward) arr.forEach(function(){ ...}, undefined, -20, -30); // run from length-20 to length-30 (so, backwards)
Of course, it would still skip the holes in sparse arrays.