Skip to content

Instantly share code, notes, and snippets.

@maxsei
Created December 5, 2022 16:37
Show Gist options
  • Select an option

  • Save maxsei/dcd70a22a6cc109f7376fc7022b3797a to your computer and use it in GitHub Desktop.

Select an option

Save maxsei/dcd70a22a6cc109f7376fc7022b3797a to your computer and use it in GitHub Desktop.
cartesian product using transducers
import * as tx from '@thi.ng/transducers';
const product = <T>(first?: T[], ...args: T[][]): IterableIterator<T[]> =>
first === undefined
? tx.asIterable([[]])
: args.length === 0
? tx.partition(1, first)
: tx.mapcat((x) => tx.map((y) => [x, ...y], product(...args)), first);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment