Created
August 28, 2022 21:39
-
-
Save dan-lee/a0d5ad72302b168ea836ba787d7f9eb1 to your computer and use it in GitHub Desktop.
Rust-like `windows` for arrays
This file contains 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
const windows = <T>(array: T[], slice = 2): T[][] => | |
Array.from({ length: array.length - slice }).reduce<T[][]>((result, _, i) => { | |
return [...result, array.slice(i, slice + i)] | |
}, []) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment