On Mon, Jan 2, 2012 at 3:56 PM, Mariusz Nowak < medikoo+mozilla....@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 > > a...@rauschma.de > > > > home: rauschma.de > > twitter: twitter.com/rauschma > > blog: 2ality.com > > > > > > > > _______________________________________________ > > es-discuss mailing list > > es-discuss@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@mozilla.org > https://mail.mozilla.org/listinfo/es-discuss >
_______________________________________________ es-discuss mailing list es-discuss@mozilla.org https://mail.mozilla.org/listinfo/es-discuss