Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 | 13x 13x 13x 13x 13x 25x 14x 11x | import { Tuple } from "@benneq/object";
/**
* Iterates over the elements of an {@link Iterable} in pairs.
*
* @remarks
* The behavior for `pairSize < 1` is not defined.
*
* @example
* Iterate in pairs
* ```ts
* const iterable = [1, 2, 3];
* const pairIterable = pairwise()(iterable);
* console.log(pairIterable); // [[1,2], [2,3]]
* ```
*
* @see {@link chunk} and {@link zip} for similar operations.
*
* @typeParam L - the length of the tuple
* @param pairSize - the optional length of the emitted tuples
* @returns the tuple emitting {@link Iterable}
*/
export const pairwise = <L extends number = 2>(pairSize: L = 2 as L) => {
if (process.env.NODE_ENV !== "production") {
console.assert(pairSize >= 1, "pairSize must be >= 1");
}
return function* <T>(
iterable: Iterable<T>,
// internal variables:
buffer: T[] = []
): IterableIterator<Tuple<T, L>> {
for (const value of iterable) {
if (buffer.push(value) >= pairSize) {
yield buffer as Tuple<T, L>;
buffer = buffer.slice(1);
}
}
};
};
|