for-of statement of sparse array

Jussi Kalliokoski jussi.kalliokoski at gmail.com
Thu Jul 5 23:49:53 PDT 2012


The only case where I've had a problem with forEach, map and friends
skipping holes is when I want a quick (to type) way to create a populated
array, say I wanted to do something like

var powersOf2 = Array(16).map((item, index) => Math.pow(2, index))

But that leads me to suggest Array.create() that would be another FP
goodie, simplifies things as `item` becomes irrelevant:

var powersOf2 = Array.create(16, (index) => Math.pow(2, index))

What do you think?

This being instead of the current:

var powersOf2 = []

for (var index=0; index<16; index++) {
  powersOf2.push(Math.pow(2, index))
}

I think it would align well with all these other array helpers.

Cheers,
Jussi

On Fri, Jul 6, 2012 at 8:22 AM, Rick Waldron <waldron.rick at gmail.com> wrote:

>
>
> On Thu, Jul 5, 2012 at 6:30 PM, Rick Waldron <waldron.rick at gmail.com>wrote:
>
>>
>> On Thursday, July 5, 2012 at 9:18 PM, Brendan Eich wrote:
>>
>> Brendan Eich wrote:
>>
>> This upholes the Array forEach (and all other extras) hole-skipping.
>> The deck is stacked against for(;;) iteration in my view.
>>
>>
>> LOL, "This upholds", of course.
>>
>> I had hoped this was a clever pun :)
>>
>>
>> Currently, devs expect for-loop and while (assuming common patterns in
>> play here) to be "the expected" way that sparse array holes are exposed --
>> so from the "give me what I most likely expect" perspective, I agree with
>> the consistency wins argument: for-of should act like for-in
>>
>
> To clarify, the behaviours I'm comparing are as follows:
>
> var i, a = [1, 2, , 4];
> for ( i in a ) {
>   console.log( i, a[i] );
> }
>
> 0 1
> 1 2
> 3 4
>
> var i, a = [1, 2, , 4];
> for ( i = 0; i < a.length; i++ ) {
>   console.log( i, a[i] );
> }
>
>  0 1
> 1 2
> 2 undefined
> 3 4
>
>
> var i = 0, a = [1, 2, , 4];
> while ( i < a.length ) {
>   console.log( i, a[i] ); i++;
> }
>
> 0 1
> 1 2
> 2 undefined
> 3 4
>
>
> Where the latter 2 require an explicit check (not present) against holes.
> So I would assume that for-of would behave like...
>
> var i, a = [1, 2, , 4];
> for ( i of a ) {
>   console.log( i );
> }
>
> 1
> 2
> 4
>
>
>
>
>>
>> Rick
>>
>>
>
> _______________________________________________
> es-discuss mailing list
> es-discuss at mozilla.org
> https://mail.mozilla.org/listinfo/es-discuss
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20120706/5c729d9d/attachment-0001.html>


More information about the es-discuss mailing list