Skip to content

Instantly share code, notes, and snippets.

@aquaductape
Created October 15, 2019 23:36
Show Gist options
  • Save aquaductape/b975d4447987fc605fb8d2909a71fc2a to your computer and use it in GitHub Desktop.
Save aquaductape/b975d4447987fc605fb8d2909a71fc2a to your computer and use it in GitHub Desktop.
Based on Rust's windows iteration method
Object.defineProperty(Array.prototype, 'windows', {
// Based on Rust's windows iteration method
// Unlike regular slice, these slices overlap, or rather panes through the array, hence the name windows
// If the size is greater than the slice it will not include it.
value: function(size) {
const length = this.length;
const arr = [];
for (let i = 0; i < length - size + 1; i++) {
const slice = [];
for (let j = i; j < i + size; j++) {
slice.push(this[j]);
}
arr.push(slice);
}
return arr;
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment