partition - nodef/extra-map GitHub Wiki

Segregate entries by test result.

Alternatives: partition, partitionAs.

function partition(x, ft)
// x:  a map
// ft: test 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.partition(x, v => v % 2 == 0);
// → [ Map(2) { 'b' => 2, 'd' => 4 }, Map(2) { 'a' => 1, 'c' => 3 } ]

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.partition(x, v => v % 2 == 1);
// → [
// →   Map(3) { 'a' => 1, 'c' => 3, 'e' => 5 },
// →   Map(2) { 'b' => 2, 'd' => 4 }
// → ]

References