Re: Array.prototype.repeat

2018-05-19 Thread Jordan Harband
perhaps http://array.build

On Sat, May 19, 2018 at 1:26 AM, Cyril Auburtin <cyril.aubur...@gmail.com>
wrote:

> pro: I think it's quite frequent to need `Array.from({length: .. }, () =>
> ...)`
> con: you can't generate dynamic data (like an array of random values)
>
> I think in the end this `Array.prototype.repeat` is not a good idea, but
> there should be something easier/less verbose than `Array.from({length:
> ..}, (_, i) => i)`
> maybe `Array.repeat(len, i => ..)` ?
>
> Le mer. 28 mars 2018 à 17:10, Jerry Schulteis <jdschult...@yahoo.com> a
> écrit :
>
>> First, Array.prototype.fill(value[, start[, end]]) already exists, so
>> you need a new name (I'll provisionally use mjrFill).
>> Second, Boolean arguments in an API are a pet peeve of mine, in
>>
>> ```js
>> [].mjrFill(['a', 'b'], 2, true)
>> ```
>> it is not obvious what the third argument means.
>>
>> Third, what was originally asked for was Array.prototype.repeat,
>> analogous to String.prototype.repeat(count), which returns a new string
>> consisting of the specified number of copies of the original, so:
>>
>> ```js
>> [0].repeat(3) // [0, 0, 0]
>> [['a', 'b']].repeat(2) // [['a', 'b'], ['a', 'b']]
>> ['a', 'b'].repeat(2) // ['a', 'b', 'a', 'b']
>>
>> [].mjrFill(arrayThatNeedsFlattening, n, true) // What does this do?
>>
>> arrayThatNeedsFlattening.flatten().repeat(n); // Hard to misunderstand.
>> ```
>>
>> On Tuesday, March 27, 2018, 7:25:07 PM CDT, Michael J. Ryan <
>> track...@gmail.com> wrote:
>>
>>
>> How about something like...
>>
>> Array.prototype.fill = function(filler, times, flatten) {
>>   var ret = [].concat(this);
>>   var len = Number(times) || 0;
>>   var (var i=0; i<len; i++) {
>> if (flatten && Array.isArray(filler)) {
>>   ret.push.apply(ret, filler);
>> } else {
>>   ret.push(filler);
>> }
>>   }
>>   return ret;
>> }
>>
>> [].fill(0, 3) // [0, 0, 0]
>> [].fill(['a', 'b'], 2) // [['a', 'b'], ['a', 'b']]
>> [].fill(['a', 'b'], 2, true) // ['a', 'b', 'a', 'b']
>>
>> --
>> Michael J. Ryan - http://tracker1.info
>>
>>
>> On Mon, Mar 26, 2018 at 12:02 PM Cyril Auburtin <cyril.aubur...@gmail.com>
>> wrote:
>>
>> > maybe fill with incrementing number?
>>
>> ```js
>> Array.from({length: 6}, (_, i) => i)
>> ```
>>
>> > Are there use cases for filling with alternating values, as in `['x',
>> 'y'].repeat(3)`?
>>
>> Not so many, but for example when working with flat matrices,
>> `[0,0,255,1].repeat(len)` for generating quickly a uniform imageData
>>
>> But even with one item, I find `[x].repeat(n)` more explicit than the 2
>> other alternatiives
>>
>> It's somewhat close to array comprehensions (that I don't really miss
>> though)
>>
>>
>> 2018-03-26 15:27 GMT+02:00 Jerry Schulteis <jdschult...@yahoo.com>:
>>
>> Whatever the use cases might be, I like generators and spread for filling
>> an array with values, e.g.:
>>
>> ```js
>> function* repeat(n, ...values) {
>>   for (let i = 0; i < n; ++i) {
>> yield* values;
>>   }
>> }
>>
>> [...repeat(3, 'x', 'y')]
>> ```
>>
>>
>> On Sunday, March 25, 2018, 3:41:10 PM CDT, Claude Pache <
>> claude.pa...@gmail.com> wrote:
>>
>>
>> [...]
>>
>> For filling a new array with one value, `Array(n).fill('foo')` seems
>> reasonable to me.
>>
>> Are there use cases for filling with alternating values, as in `['x',
>> 'y'].repeat(3)`?
>>
>>
>> ___
>> 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
>>
>
> ___
> 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


Re: Array.prototype.repeat

2018-05-19 Thread Cyril Auburtin
pro: I think it's quite frequent to need `Array.from({length: .. }, () =>
...)`
con: you can't generate dynamic data (like an array of random values)

I think in the end this `Array.prototype.repeat` is not a good idea, but
there should be something easier/less verbose than `Array.from({length:
..}, (_, i) => i)`
maybe `Array.repeat(len, i => ..)` ?

Le mer. 28 mars 2018 à 17:10, Jerry Schulteis <jdschult...@yahoo.com> a
écrit :

> First, Array.prototype.fill(value[, start[, end]]) already exists, so you
> need a new name (I'll provisionally use mjrFill).
> Second, Boolean arguments in an API are a pet peeve of mine, in
>
> ```js
> [].mjrFill(['a', 'b'], 2, true)
> ```
> it is not obvious what the third argument means.
>
> Third, what was originally asked for was Array.prototype.repeat, analogous
> to String.prototype.repeat(count), which returns a new string consisting of
> the specified number of copies of the original, so:
>
> ```js
> [0].repeat(3) // [0, 0, 0]
> [['a', 'b']].repeat(2) // [['a', 'b'], ['a', 'b']]
> ['a', 'b'].repeat(2) // ['a', 'b', 'a', 'b']
>
> [].mjrFill(arrayThatNeedsFlattening, n, true) // What does this do?
>
> arrayThatNeedsFlattening.flatten().repeat(n); // Hard to misunderstand.
> ```
>
> On Tuesday, March 27, 2018, 7:25:07 PM CDT, Michael J. Ryan <
> track...@gmail.com> wrote:
>
>
> How about something like...
>
> Array.prototype.fill = function(filler, times, flatten) {
>   var ret = [].concat(this);
>   var len = Number(times) || 0;
>   var (var i=0; i<len; i++) {
> if (flatten && Array.isArray(filler)) {
>   ret.push.apply(ret, filler);
> } else {
>   ret.push(filler);
> }
>   }
>   return ret;
> }
>
> [].fill(0, 3) // [0, 0, 0]
> [].fill(['a', 'b'], 2) // [['a', 'b'], ['a', 'b']]
> [].fill(['a', 'b'], 2, true) // ['a', 'b', 'a', 'b']
>
> --
> Michael J. Ryan - http://tracker1.info
>
>
> On Mon, Mar 26, 2018 at 12:02 PM Cyril Auburtin <cyril.aubur...@gmail.com>
> wrote:
>
> > maybe fill with incrementing number?
>
> ```js
> Array.from({length: 6}, (_, i) => i)
> ```
>
> > Are there use cases for filling with alternating values, as in `['x',
> 'y'].repeat(3)`?
>
> Not so many, but for example when working with flat matrices,
> `[0,0,255,1].repeat(len)` for generating quickly a uniform imageData
>
> But even with one item, I find `[x].repeat(n)` more explicit than the 2
> other alternatiives
>
> It's somewhat close to array comprehensions (that I don't really miss
> though)
>
>
> 2018-03-26 15:27 GMT+02:00 Jerry Schulteis <jdschult...@yahoo.com>:
>
> Whatever the use cases might be, I like generators and spread for filling
> an array with values, e.g.:
>
> ```js
> function* repeat(n, ...values) {
>   for (let i = 0; i < n; ++i) {
> yield* values;
>   }
> }
>
> [...repeat(3, 'x', 'y')]
> ```
>
>
> On Sunday, March 25, 2018, 3:41:10 PM CDT, Claude Pache <
> claude.pa...@gmail.com> wrote:
>
>
> [...]
>
> For filling a new array with one value, `Array(n).fill('foo')` seems
> reasonable to me.
>
> Are there use cases for filling with alternating values, as in `['x',
> 'y'].repeat(3)`?
>
>
> ___
> 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
>
___
es-discuss mailing list
es-discuss@mozilla.org
https://mail.mozilla.org/listinfo/es-discuss


Re: Array.prototype.repeat

2018-03-28 Thread Jerry Schulteis
First, Array.prototype.fill(value[, start[, end]]) already exists, so you need 
a new name (I'll provisionally use mjrFill).
Second, Boolean arguments in an API are a pet peeve of mine, in
```js[].mjrFill(['a', 'b'], 2, true)```it is not obvious what the third 
argument means.

Third, what was originally asked for was Array.prototype.repeat, analogous to 
String.prototype.repeat(count), which returns a new string consisting of the 
specified number of copies of the original, so:
```js[0].repeat(3) // [0, 0, 0][['a', 'b']].repeat(2) // [['a', 'b'], ['a', 
'b']]
['a', 'b'].repeat(2) // ['a', 'b', 'a', 'b']
[].mjrFill(arrayThatNeedsFlattening, n, true) // What does this do?
arrayThatNeedsFlattening.flatten().repeat(n); // Hard to misunderstand.
```
On Tuesday, March 27, 2018, 7:25:07 PM CDT, Michael J. Ryan 
<track...@gmail.com> wrote:  
 
 How about something like...
Array.prototype.fill = function(filler, times, flatten) {  var ret = 
[].concat(this);  var len = Number(times) || 0;  var (var i=0; i<len; i++) {    
if (flatten && Array.isArray(filler)) {      ret.push.apply(ret, filler);    } 
else {      ret.push(filler);    }  }  return ret;}
[].fill(0, 3) // [0, 0, 0][].fill(['a', 'b'], 2) // [['a', 'b'], ['a', 
'b']][].fill(['a', 'b'], 2, true) // ['a', 'b', 'a', 'b']
-- 
Michael J. Ryan - http://tracker1.info


On Mon, Mar 26, 2018 at 12:02 PM Cyril Auburtin <cyril.aubur...@gmail.com> 
wrote:

> maybe fill with incrementing number?
```jsArray.from({length: 6}, (_, i) => i)```
> Are there use cases for filling with alternating values, as in `['x', 
>'y'].repeat(3)`?
Not so many, but for example when working with flat matrices, 
`[0,0,255,1].repeat(len)` for generating quickly a uniform imageData
But even with one item, I find `[x].repeat(n)` more explicit than the 2 other 
alternatiives
It's somewhat close to array comprehensions (that I don't really miss though)

2018-03-26 15:27 GMT+02:00 Jerry Schulteis <jdschult...@yahoo.com>:

Whatever the use cases might be, I like generators and spread for filling an 
array with values, e.g.:
```jsfunction* repeat(n, ...values) {  for (let i = 0; i < n; ++i) {    yield* 
values;  }}
[...repeat(3, 'x', 'y')]
```


On Sunday, March 25, 2018, 3:41:10 PM CDT, Claude Pache 
<claude.pa...@gmail.com> wrote:  
 
[...]
For filling a new array with one value, `Array(n).fill('foo')` seems reasonable 
to me.
Are there use cases for filling with alternating values, as in `['x', 
'y'].repeat(3)`?
  

___
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
  ___
es-discuss mailing list
es-discuss@mozilla.org
https://mail.mozilla.org/listinfo/es-discuss


Re: Array.prototype.repeat

2018-03-27 Thread Michael J. Ryan
Or for that matter, similarly  Array.fill(), which has the same
signature, but an empty array as Array.prototype.fill.

-- 
Michael J. Ryan - http://tracker1.info


On Tue, Mar 27, 2018 at 5:24 PM Michael J. Ryan  wrote:

> How about something like...
>
> Array.prototype.fill = function(filler, times, flatten) {
>   var ret = [].concat(this);
>   var len = Number(times) || 0;
>   var (var i=0; i if (flatten && Array.isArray(filler)) {
>   ret.push.apply(ret, filler);
> } else {
>   ret.push(filler);
> }
>   }
>   return ret;
> }
>
> [].fill(0, 3) // [0, 0, 0]
> [].fill(['a', 'b'], 2) // [['a', 'b'], ['a', 'b']]
> [].fill(['a', 'b'], 2, true) // ['a', 'b', 'a', 'b']
>
> --
> Michael J. Ryan - http://tracker1.info
>
>
> On Mon, Mar 26, 2018 at 12:02 PM Cyril Auburtin 
> wrote:
>
>> > maybe fill with incrementing number?
>>
>> ```js
>> Array.from({length: 6}, (_, i) => i)
>> ```
>>
>> > Are there use cases for filling with alternating values, as in `['x',
>> 'y'].repeat(3)`?
>>
>> Not so many, but for example when working with flat matrices,
>> `[0,0,255,1].repeat(len)` for generating quickly a uniform imageData
>>
>> But even with one item, I find `[x].repeat(n)` more explicit than the 2
>> other alternatiives
>>
>> It's somewhat close to array comprehensions (that I don't really miss
>> though)
>>
>>
>> 2018-03-26 15:27 GMT+02:00 Jerry Schulteis :
>>
>>> Whatever the use cases might be, I like generators and spread for
>>> filling an array with values, e.g.:
>>>
>>> ```js
>>> function* repeat(n, ...values) {
>>>   for (let i = 0; i < n; ++i) {
>>> yield* values;
>>>   }
>>> }
>>>
>>> [...repeat(3, 'x', 'y')]
>>> ```
>>>
>>>
>>> On Sunday, March 25, 2018, 3:41:10 PM CDT, Claude Pache <
>>> claude.pa...@gmail.com> wrote:
>>>
>>>
>>> [...]
>>>
>>> For filling a new array with one value, `Array(n).fill('foo')` seems
>>> reasonable to me.
>>>
>>> Are there use cases for filling with alternating values, as in `['x',
>>> 'y'].repeat(3)`?
>>>
>>>
>> ___
>> 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


Re: Array.prototype.repeat

2018-03-27 Thread Michael J. Ryan
How about something like...

Array.prototype.fill = function(filler, times, flatten) {
  var ret = [].concat(this);
  var len = Number(times) || 0;
  var (var i=0; i
wrote:

> > maybe fill with incrementing number?
>
> ```js
> Array.from({length: 6}, (_, i) => i)
> ```
>
> > Are there use cases for filling with alternating values, as in `['x',
> 'y'].repeat(3)`?
>
> Not so many, but for example when working with flat matrices,
> `[0,0,255,1].repeat(len)` for generating quickly a uniform imageData
>
> But even with one item, I find `[x].repeat(n)` more explicit than the 2
> other alternatiives
>
> It's somewhat close to array comprehensions (that I don't really miss
> though)
>
>
> 2018-03-26 15:27 GMT+02:00 Jerry Schulteis :
>
>> Whatever the use cases might be, I like generators and spread for filling
>> an array with values, e.g.:
>>
>> ```js
>> function* repeat(n, ...values) {
>>   for (let i = 0; i < n; ++i) {
>> yield* values;
>>   }
>> }
>>
>> [...repeat(3, 'x', 'y')]
>> ```
>>
>>
>> On Sunday, March 25, 2018, 3:41:10 PM CDT, Claude Pache <
>> claude.pa...@gmail.com> wrote:
>>
>>
>> [...]
>>
>> For filling a new array with one value, `Array(n).fill('foo')` seems
>> reasonable to me.
>>
>> Are there use cases for filling with alternating values, as in `['x',
>> 'y'].repeat(3)`?
>>
>>
> ___
> 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


Re: Array.prototype.repeat

2018-03-26 Thread Cyril Auburtin
> maybe fill with incrementing number?

```js
Array.from({length: 6}, (_, i) => i)
```

> Are there use cases for filling with alternating values, as in `['x',
'y'].repeat(3)`?

Not so many, but for example when working with flat matrices,
`[0,0,255,1].repeat(len)` for generating quickly a uniform imageData

But even with one item, I find `[x].repeat(n)` more explicit than the 2
other alternatiives

It's somewhat close to array comprehensions (that I don't really miss
though)


2018-03-26 15:27 GMT+02:00 Jerry Schulteis :

> Whatever the use cases might be, I like generators and spread for filling
> an array with values, e.g.:
>
> ```js
> function* repeat(n, ...values) {
>   for (let i = 0; i < n; ++i) {
> yield* values;
>   }
> }
>
> [...repeat(3, 'x', 'y')]
> ```
>
>
> On Sunday, March 25, 2018, 3:41:10 PM CDT, Claude Pache <
> claude.pa...@gmail.com> wrote:
>
>
> [...]
>
> For filling a new array with one value, `Array(n).fill('foo')` seems
> reasonable to me.
>
> Are there use cases for filling with alternating values, as in `['x',
> 'y'].repeat(3)`?
>
>
___
es-discuss mailing list
es-discuss@mozilla.org
https://mail.mozilla.org/listinfo/es-discuss


Re: Array.prototype.repeat

2018-03-26 Thread Jerry Schulteis
Whatever the use cases might be, I like generators and spread for filling an 
array with values, e.g.:
```jsfunction* repeat(n, ...values) {  for (let i = 0; i < n; ++i) {    yield* 
values;  }}
[...repeat(3, 'x', 'y')]
```


On Sunday, March 25, 2018, 3:41:10 PM CDT, Claude Pache 
 wrote:  
 
[...]
For filling a new array with one value, `Array(n).fill('foo')` seems reasonable 
to me.
Are there use cases for filling with alternating values, as in `['x', 
'y'].repeat(3)`?
  ___
es-discuss mailing list
es-discuss@mozilla.org
https://mail.mozilla.org/listinfo/es-discuss


Re: Array.prototype.repeat

2018-03-25 Thread Isiah Meadows
Edits:

1. s/Haskell/Swift/g
2. Most of my utility has been in things like random data.

Now that I take a second look at this, maybe it's better as just a
utility function, not in the standard. (There isn't really anything
this could provide that aren't possible otherwise, and it's not as
broad of a use case as, say, `Array.prototype.map` or `Math.random`.)

-

Isiah Meadows
m...@isiahmeadows.com

Looking for web consulting? Or a new website?
Send me an email and we can get started.
www.isiahmeadows.com


On Sun, Mar 25, 2018 at 9:52 PM, Isiah Meadows  wrote:
> I like this idea, and it has plenty of precedent in other languages
> (Python, Haskell, Clojure, etc.). It's more useful for arrays in my
> experience than even for strings.
> -
>
> Isiah Meadows
> m...@isiahmeadows.com
>
> Looking for web consulting? Or a new website?
> Send me an email and we can get started.
> www.isiahmeadows.com
>
>
> On Sun, Mar 25, 2018 at 2:27 PM, Cyril Auburtin
>  wrote:
>> String and Array share a few methods.
>>
>> I think `repeat` could exist for Array as well
>>
>> At the moment are other more verbose ways to do so:
>>
>> - `Array.from({length: n}, () => 'foo')`
>> - `Array(n).fill('foo')`
>> - `[].concat(...Array.from({length: 3}, () => ['x', 'y']))`
>> - `[].concat(...Array(3).fill(['x', 'y']))`
>>
>> so with repeat it would just be;
>>
>> - `['foo'].repeat(n)`
>> - `['x', 'y'].repeat(3)`
>>
>> ___
>> 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


Re: Array.prototype.repeat

2018-03-25 Thread Isiah Meadows
Edit: that `while (i < count)` should be `while (i < end)`, where `end
= length * count`. My bad.
-

Isiah Meadows
m...@isiahmeadows.com

Looking for web consulting? Or a new website?
Send me an email and we can get started.
www.isiahmeadows.com


On Sun, Mar 25, 2018 at 10:21 PM, Isiah Meadows <isiahmead...@gmail.com> wrote:
> That *could* repeat an array, but it's not very good as a polyfill (it
> creates too many intermediate arrays). I'd expect a polyfill would be
> closer to this:
>
> ```js
> const ToInteger = x => (x = +x; x - x % 1)
>
> Array.prototype.repeat = function repeat(count) {
> count = ToInteger(count)
> let o = Object(this)
> let length = ToInteger(o.length)
> let output = new Array(length * count)
> let i = 0
> while (i < count) {
> let j = 0
> while (j < length) output[i++] = o[j++]
> }
> return output
> }
> ```
>
> And while we're at it, could we also add a `Array.prototype.set` to
> mirror `TypedArray.prototype.set`? I could've used that several times
> already, and it's an easily vectorized operation.
>
> -
>
> Isiah Meadows
> m...@isiahmeadows.com
>
> Looking for web consulting? Or a new website?
> Send me an email and we can get started.
> www.isiahmeadows.com
>
>
> On Sun, Mar 25, 2018 at 2:36 PM, Peter Jaszkowiak <p.jasz...@gmail.com> wrote:
>> I'm guessing this would be an appropriate polyfill:
>>
>> ```
>> Array.prototype.repeat = function repeat(count) {
>>   let output = this;
>>   while (--count) {
>> output = output.concat(this);
>>   }
>>   return output;
>> };
>> ```
>>
>> On Sun, Mar 25, 2018 at 12:27 PM, Cyril Auburtin <cyril.aubur...@gmail.com>
>> wrote:
>>>
>>> String and Array share a few methods.
>>>
>>> I think `repeat` could exist for Array as well
>>>
>>> At the moment are other more verbose ways to do so:
>>>
>>> - `Array.from({length: n}, () => 'foo')`
>>> - `Array(n).fill('foo')`
>>> - `[].concat(...Array.from({length: 3}, () => ['x', 'y']))`
>>> - `[].concat(...Array(3).fill(['x', 'y']))`
>>>
>>> so with repeat it would just be;
>>>
>>> - `['foo'].repeat(n)`
>>> - `['x', 'y'].repeat(3)`
>>>
>>> ___
>>> 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
>>
___
es-discuss mailing list
es-discuss@mozilla.org
https://mail.mozilla.org/listinfo/es-discuss


Re: Array.prototype.repeat

2018-03-25 Thread Isiah Meadows
That *could* repeat an array, but it's not very good as a polyfill (it
creates too many intermediate arrays). I'd expect a polyfill would be
closer to this:

```js
const ToInteger = x => (x = +x; x - x % 1)

Array.prototype.repeat = function repeat(count) {
count = ToInteger(count)
let o = Object(this)
let length = ToInteger(o.length)
let output = new Array(length * count)
let i = 0
while (i < count) {
let j = 0
while (j < length) output[i++] = o[j++]
}
return output
}
```

And while we're at it, could we also add a `Array.prototype.set` to
mirror `TypedArray.prototype.set`? I could've used that several times
already, and it's an easily vectorized operation.

-

Isiah Meadows
m...@isiahmeadows.com

Looking for web consulting? Or a new website?
Send me an email and we can get started.
www.isiahmeadows.com


On Sun, Mar 25, 2018 at 2:36 PM, Peter Jaszkowiak <p.jasz...@gmail.com> wrote:
> I'm guessing this would be an appropriate polyfill:
>
> ```
> Array.prototype.repeat = function repeat(count) {
>   let output = this;
>   while (--count) {
> output = output.concat(this);
>   }
>   return output;
> };
> ```
>
> On Sun, Mar 25, 2018 at 12:27 PM, Cyril Auburtin <cyril.aubur...@gmail.com>
> wrote:
>>
>> String and Array share a few methods.
>>
>> I think `repeat` could exist for Array as well
>>
>> At the moment are other more verbose ways to do so:
>>
>> - `Array.from({length: n}, () => 'foo')`
>> - `Array(n).fill('foo')`
>> - `[].concat(...Array.from({length: 3}, () => ['x', 'y']))`
>> - `[].concat(...Array(3).fill(['x', 'y']))`
>>
>> so with repeat it would just be;
>>
>> - `['foo'].repeat(n)`
>> - `['x', 'y'].repeat(3)`
>>
>> ___
>> 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
>
___
es-discuss mailing list
es-discuss@mozilla.org
https://mail.mozilla.org/listinfo/es-discuss


Re: Array.prototype.repeat

2018-03-25 Thread Isiah Meadows
I like this idea, and it has plenty of precedent in other languages
(Python, Haskell, Clojure, etc.). It's more useful for arrays in my
experience than even for strings.
-

Isiah Meadows
m...@isiahmeadows.com

Looking for web consulting? Or a new website?
Send me an email and we can get started.
www.isiahmeadows.com


On Sun, Mar 25, 2018 at 2:27 PM, Cyril Auburtin
 wrote:
> String and Array share a few methods.
>
> I think `repeat` could exist for Array as well
>
> At the moment are other more verbose ways to do so:
>
> - `Array.from({length: n}, () => 'foo')`
> - `Array(n).fill('foo')`
> - `[].concat(...Array.from({length: 3}, () => ['x', 'y']))`
> - `[].concat(...Array(3).fill(['x', 'y']))`
>
> so with repeat it would just be;
>
> - `['foo'].repeat(n)`
> - `['x', 'y'].repeat(3)`
>
> ___
> 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


Re: Array.prototype.repeat

2018-03-25 Thread J Decker
On Sun, Mar 25, 2018 at 1:40 PM, Claude Pache 
wrote:

>
>
> Le 25 mars 2018 à 20:27, Cyril Auburtin  a
> écrit :
>
> String and Array share a few methods.
>
> I think `repeat` could exist for Array as well
>
> At the moment are other more verbose ways to do so:
>
> - `Array.from({length: n}, () => 'foo')`
> - `Array(n).fill('foo')`
> - `[].concat(...Array.from({length: 3}, () => ['x', 'y']))`
> - `[].concat(...Array(3).fill(['x', 'y']))`
>
> so with repeat it would just be;
>
> - `['foo'].repeat(n)`
> - `['x', 'y'].repeat(3)`
>
>
> For filling a new array with one value, `Array(n).fill('foo')` seems
> reasonable to me.
>
> Are there use cases for filling with alternating values, as in `['x',
> 'y'].repeat(3)`?
>

maybe fill with incrementing number?


>
> —Claude
>
> ___
> 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


Re: Array.prototype.repeat

2018-03-25 Thread Claude Pache


> Le 25 mars 2018 à 20:27, Cyril Auburtin  a écrit :
> 
> String and Array share a few methods.
> 
> I think `repeat` could exist for Array as well
> 
> At the moment are other more verbose ways to do so:
> 
> - `Array.from({length: n}, () => 'foo')`
> - `Array(n).fill('foo')`
> - `[].concat(...Array.from({length: 3}, () => ['x', 'y']))`
> - `[].concat(...Array(3).fill(['x', 'y']))`
> 
> so with repeat it would just be;
> 
> - `['foo'].repeat(n)`
> - `['x', 'y'].repeat(3)`

For filling a new array with one value, `Array(n).fill('foo')` seems reasonable 
to me.

Are there use cases for filling with alternating values, as in `['x', 
'y'].repeat(3)`?

—Claude___
es-discuss mailing list
es-discuss@mozilla.org
https://mail.mozilla.org/listinfo/es-discuss


Re: Array.prototype.repeat

2018-03-25 Thread Peter Jaszkowiak
I'm guessing this would be an appropriate polyfill:

```
Array.prototype.repeat = function repeat(count) {
  let output = this;
  while (--count) {
output = output.concat(this);
  }
  return output;
};
```

On Sun, Mar 25, 2018 at 12:27 PM, Cyril Auburtin <cyril.aubur...@gmail.com>
wrote:

> String and Array share a few methods.
>
> I think `repeat` could exist for Array as well
>
> At the moment are other more verbose ways to do so:
>
> - `Array.from({length: n}, () => 'foo')`
> - `Array(n).fill('foo')`
> - `[].concat(...Array.from({length: 3}, () => ['x', 'y']))`
> - `[].concat(...Array(3).fill(['x', 'y']))`
>
> so with repeat it would just be;
>
> - `['foo'].repeat(n)`
> - `['x', 'y'].repeat(3)`
>
> ___
> 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


Array.prototype.repeat

2018-03-25 Thread Cyril Auburtin
String and Array share a few methods.

I think `repeat` could exist for Array as well

At the moment are other more verbose ways to do so:

- `Array.from({length: n}, () => 'foo')`
- `Array(n).fill('foo')`
- `[].concat(...Array.from({length: 3}, () => ['x', 'y']))`
- `[].concat(...Array(3).fill(['x', 'y']))`

so with repeat it would just be;

- `['foo'].repeat(n)`
- `['x', 'y'].repeat(3)`
___
es-discuss mailing list
es-discuss@mozilla.org
https://mail.mozilla.org/listinfo/es-discuss


Re: Suggestion: Array.prototype.repeat

2012-01-03 Thread Mariusz Nowak


Rick Waldron wrote:
 
 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 (...)
 

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


-
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
es-discuss@mozilla.org
https://mail.mozilla.org/listinfo/es-discuss


Re: Suggestion: Array.prototype.repeat

2012-01-03 Thread Herby Vojčík

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: es-discuss@mozilla.org ; Mariusz Nowak
Subject: Re: Suggestion: Array.prototype.repeat




On Mon, Jan 2, 2012 at 5:55 PM, Adam Shannon a...@ashannon.us 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 1 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
es-discuss@mozilla.org
https://mail.mozilla.org/listinfo/es-discuss


Re: Suggestion: Array.prototype.repeat

2012-01-03 Thread felix
repeatD(10) returns 17 copies, not 10.

On Tue, Jan 3, 2012 at 4:28 AM, Herby Vojčík he...@mailbox.sk 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: es-discuss@mozilla.org ; Mariusz Nowak
 Subject: Re: Suggestion: Array.prototype.repeat





 On Mon, Jan 2, 2012 at 5:55 PM, Adam Shannon a...@ashannon.us 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 1 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
 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


Re: Suggestion: Array.prototype.repeat

2012-01-03 Thread Rick Waldron
On Tue, Jan 3, 2012 at 3:34 AM, Mariusz Nowak 
medikoo+mozilla@medikoo.com wrote:



 Rick Waldron wrote:
 
  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 (...)
 

 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

Array.prototype.map
 - http://es5.github.com/#x15.4.4.19 #6

Array.prototype.slice
 - http://es5.github.com/#x15.4.4.10 #2

Array.prototype.splice
 - http://es5.github.com/#x15.4.4.12 #2

String.prototype.match
 - http://es5.github.com/#x15.5.4.10 #8b

String.prototype.split
 - http://es5.github.com/#x15.5.4.14 #3

RegExp.prototype.exec
 - http://es5.github.com/#x15.10.6.2 #13

Object.getOwnPropertyNames
 - http://es5.github.com/#x15.2.3.4 #2

Object.keys
 - http://es5.github.com/#x15.2.3.14 #3







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


Re: Suggestion: Array.prototype.repeat

2012-01-03 Thread Herby Vojčík
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 ; es-discuss@mozilla.org
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 he...@mailbox.sk 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: es-discuss@mozilla.org ; Mariusz Nowak
Subject: Re: Suggestion: Array.prototype.repeat





On Mon, Jan 2, 2012 at 5:55 PM, Adam Shannon a...@ashannon.us 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 1 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
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


Re: Suggestion: Array.prototype.repeat

2012-01-03 Thread Mariusz Nowak


Rick Waldron wrote:
 
 On Tue, Jan 3, 2012 at 3:34 AM, Mariusz Nowak 
 medikoo+mozilla@medikoo.com wrote:
 


 Rick Waldron wrote:
 
  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 (...)
 

 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
es-discuss@mozilla.org
https://mail.mozilla.org/listinfo/es-discuss


Re: Suggestion: Array.prototype.repeat

2012-01-03 Thread Rick Waldron
On Tue, Jan 3, 2012 at 9:19 AM, Mariusz Nowak 
medikoo+mozilla@medikoo.com wrote:



 Rick Waldron wrote:
 
  On Tue, Jan 3, 2012 at 3:34 AM, Mariusz Nowak 
  medikoo+mozilla@medikoo.com wrote:
 
 
 
  Rick Waldron wrote:
  
   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 (...)
  
 
  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
 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


Re: Suggestion: Array.prototype.repeat

2012-01-03 Thread Greg Smith
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 
medikoo+mozilla@medikoo.com wrote:



 Rick Waldron wrote:
 
  On Tue, Jan 3, 2012 at 3:34 AM, Mariusz Nowak 
  medikoo+mozilla@medikoo.com wrote:
 
 
 
  Rick Waldron wrote:
  
   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 (...)
  
 
  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
 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


Re: Suggestion: Array.prototype.repeat

2012-01-03 Thread Mariusz Nowak


Rick Waldron wrote:
 
 On Tue, Jan 3, 2012 at 9:19 AM, Mariusz Nowak 
 medikoo+mozilla@medikoo.com wrote:
 


 Rick Waldron wrote:
 
  On Tue, Jan 3, 2012 at 3:34 AM, Mariusz Nowak 
  medikoo+mozilla@medikoo.com wrote:
 
 
 
  Rick Waldron wrote:
  
   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 (...)
  
 
  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/

-
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
es-discuss@mozilla.org
https://mail.mozilla.org/listinfo/es-discuss


Re: Suggestion: Array.prototype.repeat

2012-01-03 Thread Rick Waldron
On Tue, Jan 3, 2012 at 10:16 AM, Mariusz Nowak 
medikoo+mozilla@medikoo.com wrote:



 Rick Waldron wrote:
 
  On Tue, Jan 3, 2012 at 9:19 AM, Mariusz Nowak 
  medikoo+mozilla@medikoo.com wrote:
 
 
 
  Rick Waldron wrote:
  
   On Tue, Jan 3, 2012 at 3:34 AM, Mariusz Nowak 
   medikoo+mozilla@medikoo.com wrote:
  
  
  
   Rick Waldron wrote:
   
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 (...)
   
  
   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
 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


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

2012-01-03 Thread Axel Rauschmayer
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
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


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

2012-01-03 Thread 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 a...@rauschma.de 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
 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


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

2012-01-03 Thread 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
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


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

2012-01-03 Thread Sean Eagan
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 a...@rauschma.de 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
 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




-- 
Sean Eagan
___
es-discuss mailing list
es-discuss@mozilla.org
https://mail.mozilla.org/listinfo/es-discuss


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

2012-01-03 Thread Axel Rauschmayer
Either way is fine with me. But it’s probably best to copy Python’s semantics:
http://docs.python.org/py3k/library/functions.html#range

On Jan 3, 2012, at 18:50 , Sean Eagan wrote:

 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 a...@rauschma.de 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
 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
 
 
 
 
 -- 
 Sean Eagan
 

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


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

2012-01-03 Thread Adam Shannon
I would be more in favor of something like the code below, its just a
proof of concept.

 Array.prototype.range = function (value, start, end, step) {
  if (typeof value === number)  {
 end = (start = 0  value = 0) ? Math.min(value, start) : 0;
 start = (start = 0  value = 0) ? Math.max(value, start);
// What if value  start  0, what's the default then?
 step = (arguments.length == 3) ? end : 1;
 value = ;
  }

  if (typeof value !== number) {
 start ?? 0;
 //end ?? // Again, what should be the default? this.length?
 step ?? 1;
  }

   var result = [];
   for(var i=start; i  end; i++) {
   result.push(value);
   }
   return result;
   }

On Tue, Jan 3, 2012 at 11:42, Axel Rauschmayer a...@rauschma.de 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
 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




-- 
Adam Shannon
Web Developer
University of Northern Iowa
Sophomore -- Computer Science B.S.  Mathematics
http://ashannon.us
___
es-discuss mailing list
es-discuss@mozilla.org
https://mail.mozilla.org/listinfo/es-discuss


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

2012-01-03 Thread Rick Waldron
On Tue, Jan 3, 2012 at 12:50 PM, Sean Eagan seaneag...@gmail.com wrote:

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

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


This would be an unfortunate limitation, considering real world impl's
allow negative numbers...
http://documentcloud.github.com/underscore/#range



 On Tue, Jan 3, 2012 at 11:42 AM, Axel Rauschmayer a...@rauschma.de
 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
  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
 



 --
 Sean Eagan

___
es-discuss mailing list
es-discuss@mozilla.org
https://mail.mozilla.org/listinfo/es-discuss


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

2012-01-03 Thread Maël Nison
 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.

Why use a new syntax for a simple function call ?
Afaik, there is no other standard function using an object for passing
parameters, is it ?

On 3 January 2012 18:42, Axel Rauschmayer a...@rauschma.de wrote:
 I think it's fairly common for range implementations to provide an optional
 `step` parameter


 [...]

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

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




-- 
Maël Nison
Epitech 2014, Paris - Astek
___
es-discuss mailing list
es-discuss@mozilla.org
https://mail.mozilla.org/listinfo/es-discuss


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

2012-01-03 Thread Michael A. Smith
When I first started learning JavaScript I didn't understand how

new Array(n);

worked, in that it creates an empty array with a length of n. What I
had expected was an array with n values (even if it wasn't
well-defined what those values should be). So of course my attempt to
create an array of random numbers:

var rands = (new Array(6)).map(function () { return
Math.floor(Math.random() * (1001)) }; );

fell flat. This is one case where the earlier Array.prototype.repeat
proposal had merit. Either way I think the Array.prototype.range
suggestion is useful, but I wish it could generate an array with
arbitrary values. Maybe we could create a shortcut syntax instead of
requiring .map if the *value* parameter in Adam's proposal could
alternatively be a callback? (Is it safe to assume nobody wants to use
range to create an array of functions? Maybe not, but it would surely
be useful.)

Michael A. Smith
Web Developer
True Action Network, an eBay Company


On Tue, Jan 3, 2012 at 1:12 PM, Rick Waldron waldron.r...@gmail.com wrote:


 On Tue, Jan 3, 2012 at 12:50 PM, Sean Eagan seaneag...@gmail.com wrote:

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

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


 This would be an unfortunate limitation, considering real world impl's allow
 negative numbers...
 http://documentcloud.github.com/underscore/#range



 On Tue, Jan 3, 2012 at 11:42 AM, Axel Rauschmayer a...@rauschma.de
 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
  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
 



 --
 Sean Eagan



 ___
 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


Re: Suggestion: Array.prototype.repeat

2012-01-03 Thread Nadav Shesek
Sorry - I had my email address misconfigured so it rejected my email.
Sending it again:


 Added my own repeatF() to the tests at http://jsperf.com/array-repeat/6.
 Seems to be much faster than the others, at least on V8.


 On Tue, Jan 3, 2012 at 2:28 PM, Herby Vojčík he...@mailbox.sk wrote:


  Hello,


 binary ftw. See 
 http://jsperf.com/array-**repeat/4http://jsperf.com/array-repeat/4Array.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: es-discuss@mozilla.org ; Mariusz Nowak

 Subject: Re: Suggestion: Array.prototype.repeat





 On Mon, Jan 2, 2012 at 5:55 PM, Adam Shannon a...@ashannon.us 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 1 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 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
 es-discuss@mozilla.org
 https://mail.mozilla.org/**listinfo/es-discusshttps://mail.mozilla.org/listinfo/es-discuss


___
es-discuss mailing list
es-discuss@mozilla.org
https://mail.mozilla.org/listinfo/es-discuss


Suggestion: Array.prototype.repeat

2012-01-02 Thread 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
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


Re: Suggestion: Array.prototype.repeat

2012-01-02 Thread Mariusz Nowak

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


Re: Suggestion: Array.prototype.repeat

2012-01-02 Thread Michael A. Smith
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.
 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
es-discuss@mozilla.org
https://mail.mozilla.org/listinfo/es-discuss


Re: Suggestion: Array.prototype.repeat

2012-01-02 Thread Adam Shannon
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 mich...@smith-li.com wrote:
 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.
 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
 es-discuss@mozilla.org
 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
es-discuss@mozilla.org
https://mail.mozilla.org/listinfo/es-discuss


Re: Suggestion: Array.prototype.repeat

2012-01-02 Thread Rick Waldron
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


Re: Suggestion: Array.prototype.repeat

2012-01-02 Thread Rick Waldron
On Mon, Jan 2, 2012 at 5:55 PM, Adam Shannon a...@ashannon.us 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 1 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




 On Mon, Jan 2, 2012 at 16:51, Michael A. Smith mich...@smith-li.com
 wrote:
  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.
  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
  es-discuss@mozilla.org
  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
 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