The type of values in the async iterable
The size of each window
Whether to yield a shorter final window if there aren't enough values (default: false)
A transformer function that yields arrays representing sliding windows of values
This function creates a transformer that collects values into sliding windows of a fixed size and yields each complete window as an array. It maintains a buffer that slides forward as new values arrive, yielding a new window each time the buffer reaches the specified length.
The sliding window pattern is useful for:
By default, only complete windows (of length n) are yielded. If the yieldsShorter parameter
is set to true, a final shorter window will be yielded if there aren't enough values to
fill a complete window at the end.
// Basic sliding window
const windows = await G.pipe(
G.ofIterable([1, 2, 3, 4, 5]),
G.window(3),
G.array
); // [[1, 2, 3], [2, 3, 4], [3, 4, 5]]
// Include shorter final window
const allWindows = await G.pipe(
G.ofIterable([1, 2, 3, 4]),
G.window(3, true),
G.array
); // [[1, 2, 3], [2, 3, 4], [3, 4]]
// Calculate moving averages
const movingAverages = await G.pipe(
G.ofIterable(measurements),
G.window(5),
G.map(window => window.reduce((sum, val) => sum + val, 0) / window.length),
G.array
);
// Detect patterns in sequence
const foundPatterns = await G.pipe(
G.ofIterable(sequence),
G.window(4),
G.filter(window => isPattern(window)),
G.array
);
Creates a transformer that yields sliding windows of values from an async iterable.