Created
October 15, 2019 23:36
-
-
Save aquaductape/b975d4447987fc605fb8d2909a71fc2a to your computer and use it in GitHub Desktop.
Based on Rust's windows iteration method
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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