prelude-ls
prelude-ls copied to clipboard
Sliding window for lists
Propose to add window
function for lists that provides a sliding window of a given size over the input. The result is an array of windows where is window is an array of always the given size.
Signature is:
Number -> [a] -> [[a]]
Usage:
[1 to 5] |> window 3
# => [ [ 1, 2, 3 ], [ 2, 3, 4 ], [ 3, 4, 5 ] ]
I have reservations about the name, as window
is a global variable in the browser environment.
Also, I was wondering what kind of use-cases you envision.
Could instead call it windowed
as F# does.
One common use-case is of sliding window is calculating a moving average over, for example, time series, as in:
[Math.random! for i til 30] |> window 7 |> map mean
Another suggestion for name would be windows
unless the longer sliding-window
seems clear & concise enough.
Related underscore.js proposals for _.sliding
and _.groupsOf
: https://github.com/jashkenas/underscore/issues/714 https://github.com/jashkenas/underscore/pull/696