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 | 1x 1x 15x 28x 78x 291x | import { flatten } from "../utils";
/**
* Computes the cartesian product of arrays of elements.
*
* ```javascript
* cartesianProduct([1, 2], [3, 4]) // [[1, 3], [1, 4], [2, 3], [2, 4]]
* ```
*
* @param T`
* Type of the elements in the arrays.
* @param {T[][]} sets
* The sets to use for the cartesian product.
* @returns {T[][]}
* The cartesian product of all provided sets.
*/
export function cartesianProduct<T>(...sets: T[][]): T[][] {
return sets.reduce(
(acc: T[][], set: T[]) => {
return flatten(
acc.map((x: T[]) => {
return set.map((y: T) => {
return [...x, y];
});
})
);
},
[[]] as T[][]
);
}
|