strideWindowed
Splits an array into subarrays (windows) of a specified windowSize,
moving the window by stride elements at a time.
Type
type strideWindowed = (stride: number) => (windowSize: number) => <T>(array: T[]) => T[][]
Parameters
stride(number): The step size to move the window in the input array. Must be greater than or equal to 1.windowSize(number): The size of each window.array(Array): An array of elements to split into windows.
Returns
- (Array of Arrays): An array of subarrays (windows) containing elements from the input array.
Example
const inputArray = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
const stride = 2;
const windowSize = 3;
const resultWindows = strideWindowed(stride)(windowSize)(inputArray);
// -> [[1, 2, 3], [3, 4, 5], [5, 6, 7], [7, 8, 9]]
Notes
-
The
strideparameter determines the step size to move the window in the inputarray. -
It should be a positive integer greater than or equal to 1.
-
If the
strideis less than 1, an error will be thrown. -
The
windowSizeparameter determines the size of each window. It should be a positive integer (greater than or equal to 1). -
If the
windowSizeis less than 1, an error will be thrown. -
The function splits the input
arrayinto subarrays (windows) of the specified size, moving the windowstrideelements at a time. If the input array is too short to create a window of the specified size, an empty array is included in the result. -
The input
arrayremains unaltered, and a new array of subarrays is returned. -
If the
windowSizeis greater than the length of the input array, the result will be an empty array. -
If the input
arrayis empty, an empty array is returned.