Created
April 20, 2017 00:33
-
-
Save ssippe/1f92625532eef28be6974f898efb23ef to your computer and use it in GitHub Desktop.
Typescript Cartesian Product
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
Mixed types are tricky.
Give this a try.
The key is
[...T]