Skip to content

cartesianProduct

Subhajit Sahu edited this page Dec 5, 2022 · 16 revisions

List cartesian product of maps.

Similar: cartesianProduct, zip.


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

var x = new Map([['a', 1],  ['b', 2], ['c', 3]]);
var y = new Map([['d', 10], ['e', 20]]);
[...map.cartesianProduct([x, y])];
// → [
// →   Map(2) { 'a' => 1, 'd' => 10 },
// →   Map(2) { 'a' => 1, 'e' => 20 },
// →   Map(2) { 'b' => 2, 'd' => 10 },
// →   Map(2) { 'b' => 2, 'e' => 20 },
// →   Map(2) { 'c' => 3, 'd' => 10 },
// →   Map(2) { 'c' => 3, 'e' => 20 }
// → ]

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


References