Skip to content

aperture

The aperture function returns a list of consecutive subsets of fixed size extracted from an array.

Syntax

typescript
aperture<T>(array: T[], size: number = 1): T[][];

Parameters

NameTypeDescription
arrayT[]Source array
sizenumberSize of each subset

Returns

TypeDescription
T[][]List of consecutive subsets

Examples

typescript
aperture([1, 2, 3, 4], 2); // => [[1,2],[2,3],[3,4]]
aperture([1, 2, 3], 1); // => [[1],[2],[3]]

Notes

  • If the size is greater than the length of the array, returns an empty array.
  • Useful for sliding window operations.

References

Released under the MIT License.