Skip to content

Instantly share code, notes, and snippets.

@kalisjoshua
Created October 28, 2024 20:31
Show Gist options
  • Save kalisjoshua/38d094fda2c1573da703140e296f4bc0 to your computer and use it in GitHub Desktop.
Save kalisjoshua/38d094fda2c1573da703140e296f4bc0 to your computer and use it in GitHub Desktop.
// SOURCE: https://gist.github.com/ssippe/1f92625532eef28be6974f898efb23ef?permalink_comment_id=4612830#gistcomment-4612830
export function cartesian<T>(...all: T[][]): T[][] {
return all.reduce<T[][]>(
(results, entries) =>
results
.map((result) => entries.map((entry) => result.concat([entry])))
.reduce((sub, res) => sub.concat(res), []),
[[]],
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment