Smoren / itertools-ts

TypeScript Iteration Tools Library
MIT License
46 stars 7 forks source link

[FR] Cartesian Product iterator #45

Closed mProjectsCode closed 11 months ago

mProjectsCode commented 11 months ago

It would be great to be able to iterate over the cartesian product of multiple lists.

Smoren commented 11 months ago

Hi @mProjectsCode,

Thank you for your suggestion! It was implemented in v1.27.0 release.

New features

Examples

import { set } from 'itertools-ts';

const numbers = [1, 2];
const letters = ['a', 'b'];
const chars = ['!', '?'];

for (const tuple of set.cartesianProduct(numbers, letters, chars)) {
  console.log(tuple);
}
/*
  [1, 'a', '!'],
  [1, 'a', '?'],
  [1, 'b', '!'],
  [1, 'b', '?'],
  [2, 'a', '!'],
  [2, 'a', '?'],
  [2, 'b', '!'],
  [2, 'b', '?'],
*/
import { Stream } from "itertools-ts";

const numbers = [1, 2];

const result = Stream.of(numbers)
  .cartesianProductWith(['a', 'b'], ['!', '?'])
  .toArray();
/*
[
  [1, 'a', '!'],
  [1, 'a', '?'],
  [1, 'b', '!'],
  [1, 'b', '?'],
  [2, 'a', '!'],
  [2, 'a', '?'],
  [2, 'b', '!'],
  [2, 'b', '?'],
]
*/
mProjectsCode commented 11 months ago

thanks :)