Last active
May 27, 2022 21:45
-
-
Save aryelgois/ef63bfbd8cbb5fa8cdd8cde84855d37b to your computer and use it in GitHub Desktop.
Permute elements in array
This file contains hidden or 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
export function* permute<T>(arr: T[], m: T[] = []): Generator<T[]> { | |
if (arr.length === 0) { | |
yield m | |
} else { | |
for (let i = 0; i < arr.length; i++) { | |
const curr = arr.slice() | |
const next = curr.splice(i, 1) | |
yield* permute(curr.slice(), m.concat(next)) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment