Hyperlinkv0.8.0-beta.28

Array

Array.windowconsteffect/Array.ts:2929
<N extends number>(n: N): <A>(self: Iterable<A>) => Array<TupleOf<N, A>>
<A, N extends number>(self: Iterable<A>, n: N): Array<TupleOf<N, A>>

Creates overlapping sliding windows of size n.

When to use

Use to process sequences with a moving window, such as for computing running averages or detecting patterns.

Details

Returns an empty array if n <= 0 or the array has fewer than n elements. Each window is a tuple of exactly n elements.

Example (Creating sliding windows)

import { Array } from "effect"

console.log(Array.window([1, 2, 3, 4, 5], 3)) // [[1, 2, 3], [2, 3, 4], [3, 4, 5]]
console.log(Array.window([1, 2, 3, 4, 5], 6)) // []
splittingchunksOf
Source effect/Array.ts:292913 lines
export const window: {
  <N extends number>(n: N): <A>(self: Iterable<A>) => Array<TupleOf<N, A>>
  <A, N extends number>(self: Iterable<A>, n: N): Array<TupleOf<N, A>>
} = dual(2, <A>(self: Iterable<A>, n: number): Array<Array<A>> => {
  const input = fromIterable(self)
  if (n > 0 && isReadonlyArrayNonEmpty(input)) {
    return Array.from(
      { length: input.length - (n - 1) },
      (_, index) => input.slice(index, index + n)
    )
  }
  return []
})