for-of statement of sparse array

Tab Atkins Jr. jackalmage at gmail.com
Fri Jul 6 00:28:30 PDT 2012


On Thu, Jul 5, 2012 at 11:49 PM, Jussi Kalliokoski
<jussi.kalliokoski at gmail.com> wrote:
> 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.

Once we have for-of and generators, making a range() generator that
accomplishes the same thing is trivial, and more powerful.  (Arbitrary
start/end/step.)

~TJ


More information about the es-discuss mailing list