Skip to content

Instantly share code, notes, and snippets.

@fasiha
Created October 30, 2020 04:17
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fasiha/b10461d72c3ea58865daf2b2129143f2 to your computer and use it in GitHub Desktop.
Save fasiha/b10461d72c3ea58865daf2b2129143f2 to your computer and use it in GitHub Desktop.
JavaScript/TypeScript basic combinatorics generators/iterators—because I always forget how these work
function* range(start: number, end: number) {
for (; start <= end; ++start) { yield start; }
}
function last<T>(arr: T[]) { return arr[arr.length - 1]; }
export function* numericCombinations(n: number, r: number, loc: number[] = []): IterableIterator<number[]> {
const idx = loc.length;
if (idx === r) {
yield loc;
return;
}
for (let next of range(idx ? last(loc) + 1 : 0, n - r + idx)) { yield* numericCombinations(n, r, loc.concat(next)); }
}
export function* combinations<T>(choices: T[], r: number) {
for (let idxs of numericCombinations(choices.length, r)) { yield idxs.map(i => choices[i]); }
}
export function* numericCartesianProduct(lenarr: number[]): Generator<number[]> {
let idx = lenarr.map(_ => 0);
let carry = 0;
while (!carry) {
yield idx;
carry = 1;
for (let i = 0; i < lenarr.length; i++) {
idx[i] += carry;
if (idx[i] >= lenarr[i]) {
idx[i] = 0;
carry = 1;
} else {
carry = 0;
break;
}
}
}
}
export function* cartesianProduct<T>(...arrs: T[][]) {
for (const idx of numericCartesianProduct(arrs.map(v => v.length))) {
yield idx.map((inner, outer) => arrs[outer][inner]);
}
}
export function* permutations<T>(choices: T[], k = choices.length, prefix = [] as T[]): Generator<T[]> {
if (prefix.length === k) { yield prefix; }
for (const [i, x] of choices.entries()) {
yield* permutations(choices.filter((_, j) => j !== i), k, prefix.concat(x));
}
}
@mudont
Copy link

mudont commented Aug 13, 2022

Thanks. Found this quite useful

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment