prelude-ls icon indicating copy to clipboard operation
prelude-ls copied to clipboard

Sliding window for lists

Open atifaziz opened this issue 9 years ago • 4 comments

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 ] ]

atifaziz avatar Mar 25 '15 20:03 atifaziz

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.

gkz avatar Mar 25 '15 20:03 gkz

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

atifaziz avatar Mar 25 '15 20:03 atifaziz

Another suggestion for name would be windows unless the longer sliding-window seems clear & concise enough.

atifaziz avatar Mar 25 '15 20:03 atifaziz

Related underscore.js proposals for _.sliding and _.groupsOf: https://github.com/jashkenas/underscore/issues/714 https://github.com/jashkenas/underscore/pull/696

michaelficarra avatar Mar 25 '15 21:03 michaelficarra