Skip to content

Instantly share code, notes, and snippets.

@mitac-lien
Forked from ssippe/cartesianProduct.ts
Created March 16, 2021 09:35
Show Gist options
  • Save mitac-lien/8b80f39a29fef95f1c6a48a97cbe7d40 to your computer and use it in GitHub Desktop.
Save mitac-lien/8b80f39a29fef95f1c6a48a97cbe7d40 to your computer and use it in GitHub Desktop.
Typescript Cartesian Product
const f = (a: any[], b: any[]): any[] =>
[].concat(...a.map(a2 => b.map(b2 => [].concat(a2, b2))));
export const cartesianProduct = (a: any[], b: any[], ...c: any[]) => {
if (!b || b.length === 0) {
return a;
}
const [b2, ...c2] = c;
const fab = f(a, b);
return cartesianProduct(fab, b2, c2);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment