Creating a filled array with a given length?

Herby Vojčík herby at mailbox.sk
Wed Dec 12 08:20:12 PST 2012



Rick Waldron wrote:
>
>
>
> On Wed, Dec 12, 2012 at 1:59 AM, Axel Rauschmayer <axel at rauschma.de
> <mailto:axel at rauschma.de>> wrote:
>
>     I would still love to have something like that in ES6 (loosely
>     similar to String.prototype.repeat). Once you have that, you can
>     e.g. use Array.prototype.map to do more things.
>
>     Two possibilities:
>     - Array.repeat(undefined, 3) -> [ undefined, undefined, undefined ]
>     - [ undefined ].repeat(3) -> [ undefined, undefined, undefined ]
>
>
> Array Comprehensions!
>
> This is probably wrong, so treat it more like an idea and less like a
> matter of fact:
>
> [ undefined for x of new Array(3) ].map( v => ... );
>
> [ undefined for x of [0,0,0] ].map( v => ... );

Well, for the few occasions I did something in Python, I did it this 
way, but with range(3) in place of new Array(3).

If it's not a sin to do it this way, then please allow it by adding 
range (however called) in ES6.

> Rick

Herby

P.S.: Or allow it some different way, it depends. integers().limit(3) is 
fine, too; if having infinite generator of increasing integers and 
decorator stopping after n items is better way.

P.P.S.: This open lots of ways. It can as well be 
[...infiniteStreamOf(undefined).limit(3)] (lotsOf(...) is shorter).

P.P.P.S.: That said, `looping(a,b,c)` generating a,b,c in a neverending 
loop (for any number of parameters) would be a good building block.


More information about the es-discuss mailing list