Open vicrac opened 4 years ago
I wonder if it's desirable to add sliding over Array, similar to List.sliding in Scala, or partition from Clojure:
sliding
Array
List.sliding
partition
// sliding(window, offset) sliding(2, 1)([1, 2, 3]) === [ [1, 2], [2, 3] ] sliding(2, 2)([1, 2, 3, 4]) === [ [1, 2], [3, 4] ] sliding(1, 2)([1, 2, 3]) === [[1], [3]]
It makes easy e.g. checking whether all elements are equal:
sliding(2, 1)([1, 1, 1, 1]).every(([a1, a2]) => a1 === a2) === true
or calculating moving average:
sliding(3, 1)([1, 2, 3, 4, 5]).map(avg) === [2, 4, 5]
export function sliding(window: number, offset: number): <A>(xs: ReadonlyArray<A>) => ReadonlyArray<ReadonlyArray<A>> { return xs => (xs.length < window ? [] : [xs.slice(0, window), ...sliding(window, offset)(xs.slice(offset))]) }
Looks like is named divvy in this Haskell package
🚀 Feature request
I wonder if it's desirable to add
sliding
overArray
, similar toList.sliding
in Scala, orpartition
from Clojure:It makes easy e.g. checking whether all elements are equal:
or calculating moving average:
Suggested Solution
Your environment