Skip to main content
Deno 2 is finally here 🎉️
Learn more
Module

x/fathym_common/deps.ts>slidingWindows

The Fathym Reference Architecture provides the common foundation for applications built in Typescript.
Go to Latest
function slidingWindows
import { slidingWindows } from "https://deno.land/x/fathym_common@v0.0.184/deps.ts";

Generates sliding views of the given array of the given size and returns a new array containing all of them.

If step is set, each window will start that many elements after the last window's start. (Default: 1)

If partial is set, windows will be generated for the last elements of the collection, resulting in some undefined values if size is greater than 1.

Examples

Example 1

import { slidingWindows } from "https://deno.land/std@0.224.0/collections/sliding_windows.ts";
import { assertEquals } from "https://deno.land/std@0.224.0/assert/assert_equals.ts";
const numbers = [1, 2, 3, 4, 5];

const windows = slidingWindows(numbers, 3);
assertEquals(windows, [
  [1, 2, 3],
  [2, 3, 4],
  [3, 4, 5],
]);

const windowsWithStep = slidingWindows(numbers, 3, { step: 2 });
assertEquals(windowsWithStep, [
  [1, 2, 3],
  [3, 4, 5],
]);

const windowsWithPartial = slidingWindows(numbers, 3, { partial: true });
assertEquals(windowsWithPartial, [
  [1, 2, 3],
  [2, 3, 4],
  [3, 4, 5],
  [4, 5],
  [5],
]);

Parameters

array: readonly T[]
size: number
optional
unnamed 2: { step?: number; partial?: boolean; } = [UNSUPPORTED]