Skip to content

Instantly share code, notes, and snippets.

@gabrielelana
Created January 31, 2023 16:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gabrielelana/dc0e63d54d6d471709aa72b0f213b1a2 to your computer and use it in GitHub Desktop.
Save gabrielelana/dc0e63d54d6d471709aa72b0f213b1a2 to your computer and use it in GitHub Desktop.
combineN TypeScript
const combineN = <T,>(n: number, xs: T[]): T[][] => {
if (n > xs.length) return [];
if (n == 1) return xs.map(x => [x]);
const [h, ...t] = xs;
return [
...combineN(n - 1, t).map(ts => [h, ...ts]),
...combineN(n, t)
];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment