for-of statement of sparse array

Jussi Kalliokoski jussi.kalliokoski at gmail.com
Fri Jul 6 03:28:26 PDT 2012


On Fri, Jul 6, 2012 at 10:28 AM, Tab Atkins Jr. <jackalmage at gmail.com>wrote:

> 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
>

Yes, maybe it isn't a worthy addition as is.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20120706/d27e9d28/attachment.html>


More information about the es-discuss mailing list