Created
December 5, 2022 16:37
-
-
Save maxsei/dcd70a22a6cc109f7376fc7022b3797a to your computer and use it in GitHub Desktop.
cartesian product using transducers
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
| 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