Array.prototype.repeat

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

Array.prototype.repeat

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
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss
Reply | Threaded
Open this post in threaded view
|

Re: Array.prototype.repeat

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 <[hidden email]> 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
[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: Array.prototype.repeat

Claude Pache
In reply to this post by Cyril Auburtin


Le 25 mars 2018 à 20:27, Cyril Auburtin <[hidden email]> 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
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss
Reply | Threaded
Open this post in threaded view
|

Re: Array.prototype.repeat

J Decker


On Sun, Mar 25, 2018 at 1:40 PM, Claude Pache <[hidden email]> wrote:


Le 25 mars 2018 à 20:27, Cyril Auburtin <[hidden email]> 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
[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: Array.prototype.repeat

Isiah Meadows-2
In reply to this post by Cyril Auburtin
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
[hidden email]

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
<[hidden email]> 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
> [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: Array.prototype.repeat

Isiah Meadows-2
In reply to this post by Peter Jaszkowiak
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
[hidden email]

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 <[hidden email]> 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 <[hidden email]>
> 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
>> [hidden email]
>> https://mail.mozilla.org/listinfo/es-discuss
>>
>
>
> _______________________________________________
> 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: Array.prototype.repeat

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

Isiah Meadows
[hidden email]

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 <[hidden email]> 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
> [hidden email]
>
> 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 <[hidden email]> 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 <[hidden email]>
>> 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
>>> [hidden email]
>>> https://mail.mozilla.org/listinfo/es-discuss
>>>
>>
>>
>> _______________________________________________
>> 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: Array.prototype.repeat

Isiah Meadows-2
In reply to this post by Isiah Meadows-2
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
[hidden email]

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 <[hidden email]> 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
> [hidden email]
>
> 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
> <[hidden email]> 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
>> [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: Array.prototype.repeat

Jerry Schulteis
In reply to this post by Claude Pache
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 <[hidden email]> 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
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss
Reply | Threaded
Open this post in threaded view
|

Re: Array.prototype.repeat

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 <[hidden email]>:
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 <[hidden email]> 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
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss
Reply | Threaded
Open this post in threaded view
|

Re: Array.prototype.repeat

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<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 <[hidden email]> 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 <[hidden email]>:
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 <[hidden email]> 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
[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: Array.prototype.repeat

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 <[hidden email]> 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 <[hidden email]> 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 <[hidden email]>:
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 <[hidden email]> 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
[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: Array.prototype.repeat

Jerry Schulteis
In reply to this post by Michael J. Ryan
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 <[hidden email]> 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 <[hidden email]> 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 <[hidden email]>:
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 <[hidden email]> 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
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss
_______________________________________________
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: Array.prototype.repeat

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 <[hidden email]> 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 <[hidden email]> 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 <[hidden email]> 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 <[hidden email]>:
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 <[hidden email]> 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
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss
_______________________________________________
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: Array.prototype.repeat

Jordan Harband

On Sat, May 19, 2018 at 1:26 AM, Cyril Auburtin <[hidden email]> 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 <[hidden email]> 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 <[hidden email]> 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 <[hidden email]> 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 <[hidden email]>:
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 <[hidden email]> 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
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss
_______________________________________________
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



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