@extra-lists/cartesian-product

Lists cartesian product of lists. :package: :smiley_cat: :running: :vhs: :moon: :scroll: :newspaper: :blue_book:

Usage no npm install needed!

<script type="module">
  import extraListsCartesianProduct from 'https://cdn.skypack.dev/@extra-lists/cartesian-product';
</script>

README

Lists cartesian product of lists. :package: :smiley_cat: :running: :vhs: :moon: :scroll: :newspaper: :blue_book:

Similar: cartesianProduct, zip.

This is part of package extra-lists.


lists.cartesianProduct(xs, fm);
// xs: lists
// fm: map function (vs, i)
const lists = require('extra-lists');

var x = [['a', 'b', 'c'], [1, 2, 3]];
var y = [['d', 'e'], [10, 20]];
[...lists.cartesianProduct([x, y])].map(a => [[...a[0]], [...a[1]]]);
// [
//   [ [ 'a', 'd' ], [ 1, 10 ] ],
//   [ [ 'a', 'e' ], [ 1, 20 ] ],
//   [ [ 'b', 'd' ], [ 2, 10 ] ],
//   [ [ 'b', 'e' ], [ 2, 20 ] ],
//   [ [ 'c', 'd' ], [ 3, 10 ] ],
//   [ [ 'c', 'e' ], [ 3, 20 ] ]
// ]

[...lists.cartesianProduct([x, y], a => lists.max(a))];
// [
//   [ 'd', 10 ],
//   [ 'e', 20 ],
//   [ 'd', 10 ],
//   [ 'e', 20 ],
//   [ 'd', 10 ],
//   [ 'e', 20 ]
// ]


References