Suggestion: Array.prototype.repeat

classic Classic list List threaded Threaded
26 messages Options
12
Reply | Threaded
Open this post in threaded view
|

Suggestion: Array.prototype.repeat

Axel Rauschmayer
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
[hidden email]

home: rauschma.de
twitter: twitter.com/rauschma
blog: 2ality.com



_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss
Reply | Threaded
Open this post in threaded view
|

Re: Suggestion: Array.prototype.repeat

medikoo
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.
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;
};

( 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@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
Reply | Threaded
Open this post in threaded view
|

Re: Suggestion: Array.prototype.repeat

Michael A. Smith-2
On Mon, Jan 2, 2012 at 3:56 PM, Mariusz Nowak
<[hidden email]> 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.
> Function (not method) that returns generated array may make more sense…

Is the difference in overhead between instantiating a new array and
using Array.prototype.slice.call on arguments really worth sacrificing
consistency with the proposed string.prototype.repeat and the very
clean syntax of someArray.repeat(n)?

Michael A. Smith
Web Developer
True Action Network (an eBay company)
_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss
Reply | Threaded
Open this post in threaded view
|

Re: Suggestion: Array.prototype.repeat

Adam Shannon-2
Another thing to think about is that .repeat (both on String and
Array) will be used a lot in production. So it would make sense for
each solution to be optimized for their specific case. It doesn't make
sense to slow down something as trivial as .repeat()

On Mon, Jan 2, 2012 at 16:51, Michael A. Smith <[hidden email]> wrote:

> On Mon, Jan 2, 2012 at 3:56 PM, Mariusz Nowak
> <[hidden email]> 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.
>> Function (not method) that returns generated array may make more sense…
>
> Is the difference in overhead between instantiating a new array and
> using Array.prototype.slice.call on arguments really worth sacrificing
> consistency with the proposed string.prototype.repeat and the very
> clean syntax of someArray.repeat(n)?
>
> Michael A. Smith
> Web Developer
> True Action Network (an eBay company)
> _______________________________________________
> es-discuss mailing list
> [hidden email]
> https://mail.mozilla.org/listinfo/es-discuss



--
Adam Shannon
Web Developer
University of Northern Iowa
Sophomore -- Computer Science B.S. & Mathematics
http://ashannon.us
_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss
Reply | Threaded
Open this post in threaded view
|

Re: Suggestion: Array.prototype.repeat

Rick Waldron
In reply to this post by medikoo


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


_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss
Reply | Threaded
Open this post in threaded view
|

Re: Suggestion: Array.prototype.repeat

Rick Waldron
In reply to this post by Adam Shannon-2


On Mon, Jan 2, 2012 at 5:55 PM, Adam Shannon <[hidden email]> wrote:
Another thing to think about is that .repeat (both on String and
Array) will be used a lot in production. So it would make sense for
each solution to be optimized for their specific case. It doesn't make
sense to slow down something as trivial as .repeat()

Creating a 10000 item array by repeating a 1000 item array 10 times, the difference is negligible when compared the implementation that I wrote, not the one given above


Also, note that I had to "make up" my own Array.repeat() because the Array.generate shown above did not create the same return as the initial Array.prototype.repeat(), but I stuck to the concepts laid out - no new array is initialized (except that Array.prototype.slice _does_ initialize a new Array() ). 

Rick

 

On Mon, Jan 2, 2012 at 16:51, Michael A. Smith <[hidden email]> wrote:
> On Mon, Jan 2, 2012 at 3:56 PM, Mariusz Nowak
> <[hidden email]> 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.
>> Function (not method) that returns generated array may make more sense…
>
> Is the difference in overhead between instantiating a new array and
> using Array.prototype.slice.call on arguments really worth sacrificing
> consistency with the proposed string.prototype.repeat and the very
> clean syntax of someArray.repeat(n)?
>
> Michael A. Smith
> Web Developer
> True Action Network (an eBay company)
> _______________________________________________
> es-discuss mailing list
> [hidden email]
> https://mail.mozilla.org/listinfo/es-discuss



--
Adam Shannon
Web Developer
University of Northern Iowa
Sophomore -- Computer Science B.S. & Mathematics
http://ashannon.us
_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss


_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss
Reply | Threaded
Open this post in threaded view
|

Re: Suggestion: Array.prototype.repeat

medikoo
In reply to this post by Rick Waldron
Rick Waldron wrote
On Mon, Jan 2, 2012 at 3:56 PM, Mariusz Nowak <
medikoo+mozilla.org@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 (...)
Rick, you say that apart of output arrays this methods create some other temporary arrays internally ?
I don't see anything like that in specification, where exactly is it stated ?

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 ] ]
It is supposed to be used that way:
Array.generate(4, 1, 2, 3) // -> [1, 2, 3, 1];

Usage you suggested as with Array.prototype.repeat will imply need of creating temporary array object.

--
Mariusz Nowak
https://github.com/medikoo
Reply | Threaded
Open this post in threaded view
|

Re: Suggestion: Array.prototype.repeat

Herby Vojčík
In reply to this post by Rick Waldron
Hello,

binary ftw. See http://jsperf.com/array-repeat/4 Array.prototype.repeatD.
And I also tried push.apply in repeatC (not to copy over the array using
concat but grow it in place until possible) and it really surprised me it
was that much slower. Concat is probably heavily optimized.

Herby

-----Pôvodná správa-----
From: Rick Waldron
Sent: Tuesday, January 03, 2012 2:21 AM
To: Adam Shannon
Cc: [hidden email] ; Mariusz Nowak
Subject: Re: Suggestion: Array.prototype.repeat




On Mon, Jan 2, 2012 at 5:55 PM, Adam Shannon <[hidden email]> wrote:
Another thing to think about is that .repeat (both on String and
Array) will be used a lot in production. So it would make sense for
each solution to be optimized for their specific case. It doesn't make
sense to slow down something as trivial as .repeat()


Creating a 10000 item array by repeating a 1000 item array 10 times, the
difference is negligible when compared the implementation that I wrote, not
the one given above

http://jsperf.com/array-repeat/2

Also, note that I had to "make up" my own Array.repeat() because the
Array.generate shown above did not create the same return as the initial
Array.prototype.repeat(), but I stuck to the concepts laid out - no new
array is initialized (except that Array.prototype.slice _does_ initialize a
new Array() ).

Rick


_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss
Reply | Threaded
Open this post in threaded view
|

Re: Suggestion: Array.prototype.repeat

Felix-54
repeatD(10) returns 17 copies, not 10.

On Tue, Jan 3, 2012 at 4:28 AM, Herby Vojčík <[hidden email]> wrote:

> Hello,
>
> binary ftw. See http://jsperf.com/array-repeat/4 Array.prototype.repeatD.
> And I also tried push.apply in repeatC (not to copy over the array using
> concat but grow it in place until possible) and it really surprised me it
> was that much slower. Concat is probably heavily optimized.
>
> Herby
>
> -----Pôvodná správa----- From: Rick Waldron
> Sent: Tuesday, January 03, 2012 2:21 AM
> To: Adam Shannon
> Cc: [hidden email] ; Mariusz Nowak
> Subject: Re: Suggestion: Array.prototype.repeat
>
>
>
>
>
> On Mon, Jan 2, 2012 at 5:55 PM, Adam Shannon <[hidden email]> wrote:
> Another thing to think about is that .repeat (both on String and
> Array) will be used a lot in production. So it would make sense for
> each solution to be optimized for their specific case. It doesn't make
> sense to slow down something as trivial as .repeat()
>
>
> Creating a 10000 item array by repeating a 1000 item array 10 times, the
> difference is negligible when compared the implementation that I wrote, not
> the one given above
>
> http://jsperf.com/array-repeat/2
>
> Also, note that I had to "make up" my own Array.repeat() because the
> Array.generate shown above did not create the same return as the initial
> Array.prototype.repeat(), but I stuck to the concepts laid out - no new
> array is initialized (except that Array.prototype.slice _does_ initialize a
> new Array() ).
>
> Rick
>
>
> _______________________________________________
> es-discuss mailing list
> [hidden email]
> https://mail.mozilla.org/listinfo/es-discuss
_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss
Reply | Threaded
Open this post in threaded view
|

Re: Suggestion: Array.prototype.repeat

Rick Waldron
In reply to this post by medikoo


On Tue, Jan 3, 2012 at 3:34 AM, Mariusz Nowak <[hidden email]> wrote:


Rick Waldron wrote:
>
> On Mon, Jan 2, 2012 at 3:56 PM, Mariusz Nowak <
> [hidden email]> 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 (...)
>

Rick, you say that apart of output arrays this methods create some other
temporary arrays internally ?
I don't see anything like that in specification, where exactly is it stated
?


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







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

It is supposed to be used that way:
Array.generate(4, 1, 2, 3) // -> [1, 2, 3, 1];

Got it, I misunderstood, as there was no example usage - thanks for clarifying.

Rick
 

Usage you suggested as with Array.prototype.repeat will imply need of
creating temporary array object.

--
Mariusz Nowak
https://github.com/medikoo


-----
Mariusz Nowak

https://github.com/medikoo
--
View this message in context: http://old.nabble.com/Suggestion%3A-Array.prototype.repeat-tp33067649p33070367.html
Sent from the Mozilla - ECMAScript 4 discussion mailing list archive at Nabble.com.

_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss


_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss
Reply | Threaded
Open this post in threaded view
|

Re: Suggestion: Array.prototype.repeat

Herby Vojčík
In reply to this post by Felix-54
Sorry, fixed (you get the idea, anyway, doing 10 concats is worse than doing
five of them, with _big_ chunks of data in the last calls). There is a
checking throw in the end so it really works now.
Now it is even more faster :-)

Herby

-----Pôvodná správa-----
From: felix
Sent: Tuesday, January 03, 2012 2:30 PM
To: Herby Vojčík
Cc: Rick Waldron ; Adam Shannon ; Mariusz Nowak ; [hidden email]
Subject: Re: Suggestion: Array.prototype.repeat

repeatD(10) returns 17 copies, not 10.

On Tue, Jan 3, 2012 at 4:28 AM, Herby Vojčík <[hidden email]> wrote:

> Hello,
>
> binary ftw. See http://jsperf.com/array-repeat/4 Array.prototype.repeatD.
> And I also tried push.apply in repeatC (not to copy over the array using
> concat but grow it in place until possible) and it really surprised me it
> was that much slower. Concat is probably heavily optimized.
>
> Herby
>
> -----Pôvodná správa----- From: Rick Waldron
> Sent: Tuesday, January 03, 2012 2:21 AM
> To: Adam Shannon
> Cc: [hidden email] ; Mariusz Nowak
> Subject: Re: Suggestion: Array.prototype.repeat
>
>
>
>
>
> On Mon, Jan 2, 2012 at 5:55 PM, Adam Shannon <[hidden email]> wrote:
> Another thing to think about is that .repeat (both on String and
> Array) will be used a lot in production. So it would make sense for
> each solution to be optimized for their specific case. It doesn't make
> sense to slow down something as trivial as .repeat()
>
>
> Creating a 10000 item array by repeating a 1000 item array 10 times, the
> difference is negligible when compared the implementation that I wrote,
> not
> the one given above
>
> http://jsperf.com/array-repeat/2
>
> Also, note that I had to "make up" my own Array.repeat() because the
> Array.generate shown above did not create the same return as the initial
> Array.prototype.repeat(), but I stuck to the concepts laid out - no new
> array is initialized (except that Array.prototype.slice _does_ initialize
> a
> new Array() ).
>
> Rick
>
>
> _______________________________________________
> es-discuss mailing list
> [hidden email]
> https://mail.mozilla.org/listinfo/es-discuss 

_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss
Reply | Threaded
Open this post in threaded view
|

Re: Suggestion: Array.prototype.repeat

medikoo
In reply to this post by Rick Waldron
Rick Waldron wrote
On Tue, Jan 3, 2012 at 3:34 AM, Mariusz Nowak <
medikoo+mozilla.org@medikoo.com> wrote:

>
>
> Rick Waldron wrote:
> >
> > On Mon, Jan 2, 2012 at 3:56 PM, Mariusz Nowak <
> > medikoo+mozilla.org@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 (...)
> >
>
> Rick, you say that apart of output arrays this methods create some other
> temporary arrays internally ?
> I don't see anything like that in specification, where exactly is it stated
> ?
>


Array.prototype.concat
 - http://es5.github.com/#x15.4.4.4 #2

Array.prototype.filter
 - http://es5.github.com/#x15.4.4.20 #6

(...)
Rick, those arrays become result of each method, they're not temporary.
Reply | Threaded
Open this post in threaded view
|

Re: Suggestion: Array.prototype.repeat

Rick Waldron


On Tue, Jan 3, 2012 at 9:19 AM, Mariusz Nowak <[hidden email]> wrote:


Rick Waldron wrote:
>
> On Tue, Jan 3, 2012 at 3:34 AM, Mariusz Nowak <
> [hidden email]> wrote:
>
>>
>>
>> Rick Waldron wrote:
>> >
>> > On Mon, Jan 2, 2012 at 3:56 PM, Mariusz Nowak <
>> > [hidden email]> 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 (...)
>> >
>>
>> Rick, you say that apart of output arrays this methods create some other
>> temporary arrays internally ?
>> I don't see anything like that in specification, where exactly is it
>> stated
>> ?
>>
>
>
> Array.prototype.concat
>  - http://es5.github.com/#x15.4.4.4 #2
>
> Array.prototype.filter
>  - http://es5.github.com/#x15.4.4.20 #6
>
> (...)
>

Rick, those arrays become result of each method, they're not temporary.

Sorry, I should've been clearer... I was responding to your statement that implied Axel's example code was somehow creating an unnecessary temporary array - have I missed something here?
 


-----
Mariusz Nowak

https://github.com/medikoo
--
View this message in context: http://old.nabble.com/Suggestion%3A-Array.prototype.repeat-tp33067649p33072157.html
Sent from the Mozilla - ECMAScript 4 discussion mailing list archive at Nabble.com.

_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss


_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss
Reply | Threaded
Open this post in threaded view
|

Re: Suggestion: Array.prototype.repeat

Greg Smith-24
In reply to this post by medikoo
What is the use case for .repeat? Trying to imagine some code where I'd need it so I can get a feel for how it should work.

On Tue, Jan 3, 2012 at 9:19 AM, Mariusz Nowak <[hidden email]> wrote:


Rick Waldron wrote:
>
> On Tue, Jan 3, 2012 at 3:34 AM, Mariusz Nowak <
> [hidden email]> wrote:
>
>>
>>
>> Rick Waldron wrote:
>> >
>> > On Mon, Jan 2, 2012 at 3:56 PM, Mariusz Nowak <
>> > [hidden email]> 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 (...)
>> >
>>
>> Rick, you say that apart of output arrays this methods create some other
>> temporary arrays internally ?
>> I don't see anything like that in specification, where exactly is it
>> stated
>> ?
>>
>
>
> Array.prototype.concat
>  - http://es5.github.com/#x15.4.4.4 #2
>
> Array.prototype.filter
>  - http://es5.github.com/#x15.4.4.20 #6
>
> (...)
>

Rick, those arrays become result of each method, they're not temporary.


-----
Mariusz Nowak

https://github.com/medikoo
--
View this message in context: http://old.nabble.com/Suggestion%3A-Array.prototype.repeat-tp33067649p33072157.html
Sent from the Mozilla - ECMAScript 4 discussion mailing list archive at Nabble.com.

_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss


_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss
Reply | Threaded
Open this post in threaded view
|

Re: Suggestion: Array.prototype.repeat

medikoo
In reply to this post by Rick Waldron
Rick Waldron wrote
On Tue, Jan 3, 2012 at 9:19 AM, Mariusz Nowak <
medikoo+mozilla.org@medikoo.com> wrote:

>
>
> Rick Waldron wrote:
> >
> > On Tue, Jan 3, 2012 at 3:34 AM, Mariusz Nowak <
> > medikoo+mozilla.org@medikoo.com> wrote:
> >
> >>
> >>
> >> Rick Waldron wrote:
> >> >
> >> > On Mon, Jan 2, 2012 at 3:56 PM, Mariusz Nowak <
> >> > medikoo+mozilla.org@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 (...)
> >> >
> >>
> >> Rick, you say that apart of output arrays this methods create some other
> >> temporary arrays internally ?
> >> I don't see anything like that in specification, where exactly is it
> >> stated
> >> ?
> >>
> >
> >
> > Array.prototype.concat
> >  - http://es5.github.com/#x15.4.4.4 #2
> >
> > Array.prototype.filter
> >  - http://es5.github.com/#x15.4.4.20 #6
> >
> > (...)
> >
>
> Rick, those arrays become result of each method, they're not temporary.
>

Sorry, I should've been clearer... I was responding to your statement that
implied Axel's example code was somehow creating an unnecessary temporary
array - have I missed something here?
Rick, what I meant is that it implies creation of temporary array object. Let's say I want to have an array made of numbers 1,2,3 repeated three times. To get it I need to create temporary [1, 2, 3] array which I don't really need:
result = [1, 2, 3].repeat(3);

It'll be more clean if it will work directly on context array:

var x = [1, 2, 3];
x.repeat(2);
console.log(x); // [1, 2, 3, 1, 2, 3];

but that probably won't be expected behavior by most developers.

Anyway as Greg pointed, it's hard to find a valid use case for any array repeating method, it conceptually may look as nice add-on, but I'm not sure if it would be that useful to have it in standard.

--
Mariusz Nowak
https://github.com/medikoo/
Reply | Threaded
Open this post in threaded view
|

Re: Suggestion: Array.prototype.repeat

Rick Waldron


On Tue, Jan 3, 2012 at 10:16 AM, Mariusz Nowak <[hidden email]> wrote:


Rick Waldron wrote:
>
> On Tue, Jan 3, 2012 at 9:19 AM, Mariusz Nowak <
> [hidden email]> wrote:
>
>>
>>
>> Rick Waldron wrote:
>> >
>> > On Tue, Jan 3, 2012 at 3:34 AM, Mariusz Nowak <
>> > [hidden email]> wrote:
>> >
>> >>
>> >>
>> >> Rick Waldron wrote:
>> >> >
>> >> > On Mon, Jan 2, 2012 at 3:56 PM, Mariusz Nowak <
>> >> > [hidden email]> 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 (...)
>> >> >
>> >>
>> >> Rick, you say that apart of output arrays this methods create some
>> other
>> >> temporary arrays internally ?
>> >> I don't see anything like that in specification, where exactly is it
>> >> stated
>> >> ?
>> >>
>> >
>> >
>> > Array.prototype.concat
>> >  - http://es5.github.com/#x15.4.4.4 #2
>> >
>> > Array.prototype.filter
>> >  - http://es5.github.com/#x15.4.4.20 #6
>> >
>> > (...)
>> >
>>
>> Rick, those arrays become result of each method, they're not temporary.
>>
>
> Sorry, I should've been clearer... I was responding to your statement that
> implied Axel's example code was somehow creating an unnecessary temporary
> array - have I missed something here?
>
>

Rick, what I meant is that it implies creation of temporary array object.
Let's say I want to have an array made of numbers 1,2,3 repeated three
times. To get it I need to create temporary [1, 2, 3] array which I don't
really need:
result = [1, 2, 3].repeat(3);

It'll be more clean if it will work directly on context array:

var x = [1, 2, 3];
x.repeat(2);
console.log(x); // [1, 2, 3, 1, 2, 3];

but that probably won't be expected behavior by most developers.

Thanks for clarifying - I had been under the impression that you were referring to the implementation details specifically.

 

Anyway as Greg pointed, it's hard to find a valid use case for any array
repeating method, it conceptually may look as nice add-on, but I'm not sure
if it would be that useful to have it in standard.

+1
 

--
Mariusz Nowak
https://github.com/medikoo/

-----
Mariusz Nowak

https://github.com/medikoo
--
View this message in context: http://old.nabble.com/Suggestion%3A-Array.prototype.repeat-tp33067649p33072532.html
Sent from the Mozilla - ECMAScript 4 discussion mailing list archive at Nabble.com.

_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss


_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss
Reply | Threaded
Open this post in threaded view
|

Array.range() (was: Suggestion: Array.prototype.repeat)

Axel Rauschmayer
In reply to this post by Greg Smith-24
On Jan 3, 2012, at 15:46 , Greg Smith wrote:

> What is the use case for .repeat? Trying to imagine some code where I'd need it so I can get a feel for how it should work.

So beauty alone does not count? ;-)

It’s true – there are not a lot of use cases for Array.repeat().

But I keep thinking that there should be a way to create an array of a given length *with* content in it. Rationale: such an array is nice to have as a starting point for a transformation via Array.prototype.map() or an array comprehension [1]. How about the following?

    Array.range = function (start, end) {
        if (arguments.length < 1 || arguments.length > 2) {
            throw new TypeError("Need one or two arguments");
        }
        if (arguments.length === 1) {
            end = start;
            start = 0;
        }
        var result = [];
        for(var i=start; i < end; i++) {
            result.push(i);
        }
        return result;
    }

Interaction:

    > Array.range(4)
    [ 0, 1, 2, 3 ]
    > Array.range(4).map(function () { return "*" })
    [ '*', '*', '*', '*' ]

I’m not yet sure how many use cases there are (suggestions welcome), but it does fill a hole (IIRC, Python has something similar).

TODO: This method probably makes more sense as an iterator (e.g. implemented via a generator). Then one could even omit the upper limit and produce an unlimited sequence.

[1] http://wiki.ecmascript.org/doku.php?id=harmony:array_comprehensions

--
Dr. Axel Rauschmayer
[hidden email]

home: rauschma.de
twitter: twitter.com/rauschma
blog: 2ality.com



_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss
Reply | Threaded
Open this post in threaded view
|

Re: Array.range() (was: Suggestion: Array.prototype.repeat)

Rick Waldron
I think it's fairly common for range implementations to provide an optional `step` parameter

On Tue, Jan 3, 2012 at 12:08 PM, Axel Rauschmayer <[hidden email]> wrote:
On Jan 3, 2012, at 15:46 , Greg Smith wrote:

> What is the use case for .repeat? Trying to imagine some code where I'd need it so I can get a feel for how it should work.

So beauty alone does not count? ;-)

It’s true – there are not a lot of use cases for Array.repeat().

But I keep thinking that there should be a way to create an array of a given length *with* content in it. Rationale: such an array is nice to have as a starting point for a transformation via Array.prototype.map() or an array comprehension [1]. How about the following?

   Array.range = function (start, end) {
       if (arguments.length < 1 || arguments.length > 2) {
           throw new TypeError("Need one or two arguments");
       }
       if (arguments.length === 1) {
           end = start;
           start = 0;
       }
       var result = [];
       for(var i=start; i < end; i++) {
           result.push(i);
       }
       return result;
   }

Interaction:

   > Array.range(4)
   [ 0, 1, 2, 3 ]
   > Array.range(4).map(function () { return "*" })
   [ '*', '*', '*', '*' ]

I’m not yet sure how many use cases there are (suggestions welcome), but it does fill a hole (IIRC, Python has something similar).

TODO: This method probably makes more sense as an iterator (e.g. implemented via a generator). Then one could even omit the upper limit and produce an unlimited sequence.

[1] http://wiki.ecmascript.org/doku.php?id=harmony:array_comprehensions

--
Dr. Axel Rauschmayer
[hidden email]

home: rauschma.de
twitter: twitter.com/rauschma
blog: 2ality.com





_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss
Reply | Threaded
Open this post in threaded view
|

Re: Array.range() (was: Suggestion: Array.prototype.repeat)

Axel Rauschmayer
I think it's fairly common for range implementations to provide an optional `step` parameter

Good point. Maybe all of these parameters should be options:

Array.range() -> 0, 1, 2, 3, ...
Array.range({ start: 3 }) -> 3, 4, 5, 6, ...
Array.range({ end: 5 }) -> 0, 1, 2, 3, 4
Array.range({ step: 3 }) -> 0, 3, 6, 9, ...
Array.range({ start: -2, step: -1 }) -> -2, -3, -4, -5, ...
etc.

The defaults of start and end and the comparison operator depend on the sign of step.

-- 
Dr. Axel Rauschmayer





_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss
Reply | Threaded
Open this post in threaded view
|

Re: Array.range() (was: Suggestion: Array.prototype.repeat)

seaneagan1
I think step should be > 0, and step towards end:

Array.range({start: 5, end: 0, step: 2}) -> 5, 3, 1

On Tue, Jan 3, 2012 at 11:42 AM, Axel Rauschmayer <[hidden email]> wrote:

> I think it's fairly common for range implementations to provide an optional
> `step` parameter
>
>
> Good point. Maybe all of these parameters should be options:
>
> Array.range() -> 0, 1, 2, 3, ...
> Array.range({ start: 3 }) -> 3, 4, 5, 6, ...
> Array.range({ end: 5 }) -> 0, 1, 2, 3, 4
> Array.range({ step: 3 }) -> 0, 3, 6, 9, ...
> Array.range({ start: -2, step: -1 }) -> -2, -3, -4, -5, ...
> etc.
>
> The defaults of start and end and the comparison operator depend on the sign
> of step.
>
> --
> Dr. Axel Rauschmayer
> [hidden email]
>
> home: rauschma.de
> twitter: twitter.com/rauschma
> blog: 2ality.com
>
>
>
>
> _______________________________________________
> es-discuss mailing list
> [hidden email]
> https://mail.mozilla.org/listinfo/es-discuss
>



--
Sean Eagan
_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss
12