Created
January 31, 2023 16:02
-
-
Save gabrielelana/dc0e63d54d6d471709aa72b0f213b1a2 to your computer and use it in GitHub Desktop.
combineN TypeScript
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 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