Suggestion: Array.prototype.repeat

Rick Waldron waldron.rick at gmail.com
Mon Jan 2 16:56:13 PST 2012


On Mon, Jan 2, 2012 at 3:56 PM, Mariusz Nowak <
medikoo+mozilla.org at medikoo.com> wrote:

>
> I like it, it indeed looks very logical, however it's a bit controversial
> that we need to create temporary array object to get one that we want.
>

Is the controversy editorial or fact, because the following methods are all
specified to use a temporary, newly initialized Array internally:

Array.prototype.concat
Array.prototype.filter
Array.prototype.map
Array.prototype.slice
Array.prototype.splice

String.prototype.match
String.prototype.split

RegExp.prototype.exec

Object.getOwnPropertyNames
Object.keys




> Function (not method) that returns generated array may make more sense,
> currently I'm using something like that:
>
> var slice = Array.prototype.slice;
>
> Array.generate = function (length, fill) {
>        var arr, l;
>        length = length >>> 0;
>        if (arguments.length < 2) {
>                throw new TypeError("Cannot generarte an array without
> provided fill.");
>        }
>        arr = slice.call(arguments, 1, 1 + length);
>        while ((l = arr.length) < length) {
>                arr = arr.concat(arr.slice(0, length - l));
>        }
>        return arr;
> };
>

This doesn't produce the same as Array.prototype.repeat..

// regular
console.log( Array.generate( 3, [1,2,3] ) );
// sparse
console.log( Array.generate( 3, [1,2,,3] ) );

[ [ 1, 2, 3 ], [ 1, 2, 3 ], [ 1, 2, 3 ] ]
[ [ 1, 2, , 3 ], [ 1, 2, , 3 ], [ 1, 2, , 3 ] ]




>
> ( https://github.com/medikoo/es5-ext/blob/master/lib/Array/generate.js )
>
> --
> Mariusz Nowak
> https://github.com/medikoo/
>
>
> rauschma wrote:
> >
> > Array.prototype.repeat seems like a logical dual to
> > String.prototype.repeat:
> >
> http://wiki.ecmascript.org/doku.php?id=harmony:string.prototype.repeat
> >
> > Implementation:
> >
> >     Array.prototype.repeat = function (times) {
> >         var result = [];
> >         var len = this.length;
> >         var resultLen = len * times;
> >         for(var i = 0; i < resultLen; i++) {
> >             result.push(this[i % len]);
> >         }
> >         return result;
> >     }
> >
> > In use:
> >
> >     $ [1,2,3].repeat(3)
> >     [ 1,  2,  3,  1,  2,  3,  1,  2,  3 ]
> >
> > --
> > Dr. Axel Rauschmayer
> > axel at rauschma.de
> >
> > home: rauschma.de
> > twitter: twitter.com/rauschma
> > blog: 2ality.com
> >
> >
> >
> > _______________________________________________
> > es-discuss mailing list
> > es-discuss at mozilla.org
> > https://mail.mozilla.org/listinfo/es-discuss
> >
> >
>
>
> -----
> Mariusz Nowak
>
> https://github.com/medikoo
> --
> View this message in context:
> http://old.nabble.com/Suggestion%3A-Array.prototype.repeat-tp33067649p33068241.html
> Sent from the Mozilla - ECMAScript 4 discussion mailing list archive at
> Nabble.com.
>
> _______________________________________________
> 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/20120102/2b61250b/attachment-0001.html>


More information about the es-discuss mailing list