@extra-entries/cartesian-product

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

Usage no npm install needed!

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

README

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

Similar: cartesianProduct, zip.

This is part of package extra-entries.


entries.cartesianProduct(xs, fm);
// xs: n entries
// fm: map function (vs, i)
const entries = require("extra-entries");

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

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


References