Proposal: `Array.prototype.chunks` and `Array.prototype.windows`

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

Proposal: `Array.prototype.chunks` and `Array.prototype.windows`

Joseph Rocca
Apologies if this has already been suggested and discussed somewhere, but I couldn't find anything. I'm learning Rust at the moment, and found myself wishing that JavaScript had `chunks` and `windows` array methods.


A `swap` method (like Rust's) might be handy too. Has there been any discussion on new array methods like this? I think it's great that slice notation and range() type stuff is being discussed, and I think it would be neat if JavaScript got some more array-manipulation features to make wrangling data more pleasant.

Any thoughts?

Cheers,
Joe

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

Re: Proposal: `Array.prototype.chunks` and `Array.prototype.windows`

Cyril Auburtin
Nice, https://github.com/tc39/proposal-slice-notation/issues/19#issuecomment-421453934 is really awesome. I hope this idea doesn't get abandoned

I feel you can swap things easily already;
```js
let x = 1, y = 2;
[x, y] = [y, x];
```

Chunks could be written
```js
// take chunks of 4 from arr
// with slice+range operator proposal
const chunks = [0:Math.ceil(arr.length/4)].map(i => arr[4*i:4*(i+1)])
// vs
const chunks = Array.from({length: Math.ceil(arr.length/4)}, (_,i) => arr.slice(4*i, 4*(i+1)))
```


On Tue, May 28, 2019 at 12:05 PM Joseph Rocca <[hidden email]> wrote:
Apologies if this has already been suggested and discussed somewhere, but I couldn't find anything. I'm learning Rust at the moment, and found myself wishing that JavaScript had `chunks` and `windows` array methods.


A `swap` method (like Rust's) might be handy too. Has there been any discussion on new array methods like this? I think it's great that slice notation and range() type stuff is being discussed, and I think it would be neat if JavaScript got some more array-manipulation features to make wrangling data more pleasant.

Any thoughts?

Cheers,
Joe
_______________________________________________
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