Skip to content

Instantly share code, notes, and snippets.

@bluurn
Created February 5, 2019 10:15
Show Gist options
  • Save bluurn/c2092cb5d48aa2dd05a8f407c73e1ca5 to your computer and use it in GitHub Desktop.
Save bluurn/c2092cb5d48aa2dd05a8f407c73e1ca5 to your computer and use it in GitHub Desktop.
JS: Implementation of Cartesian product
const cartesian = (...arrays) => arrays.reduce((cartesianPart, array) => (
[].concat(...
cartesianPart.map(cartesianPartTuple =>
array.map(it => cartesianPartTuple.concat(it))
)
)
), [[]]);
console.log(cartesian([1,2], [3,4], [5,6]))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment