partitionAs - nodef/extra-map GitHub Wiki

Segregate entries by similarity.

Alternatives: partition, partitionAs.

function partitionAs(x, fm)
// x:  a map
// fm: map function (v, k, x)
const map = require('extra-map');

var x = new Map(['a', 1], ['b', 2], ['c', 3], ['d', 4](/nodef/extra-map/wiki/'a',-1],-['b',-2],-['c',-3],-['d',-4));
map.partitionAs(x, v => v % 2 == 0);
// → Map(2) {
// →   false => Map(2) { 'a' => 1, 'c' => 3 },
// →   true => Map(2) { 'b' => 2, 'd' => 4 }
// → }

var x = new Map(['a', 1], ['b', 2], ['c', 3], ['d', 4], ['e', 5](/nodef/extra-map/wiki/'a',-1],-['b',-2],-['c',-3],-['d',-4],-['e',-5));
map.partitionAs(x, v => v % 3);
// → Map(3) {
// →   1 => Map(2) { 'a' => 1, 'd' => 4 },
// →   2 => Map(2) { 'b' => 2, 'e' => 5 },
// →   0 => Map(1) { 'c' => 3 }
// → }

References