Skip to content

partition

Subhajit Sahu edited this page Dec 22, 2022 · 13 revisions

Segregate values by test result.

Alternatives: partition, partitionAs.


function partition(x, ft)
// x:  ientries
// ft: test function (v, k, x)
const ientries = require('extra-ientries');

var x = [["a", 1], ["b", 2], ["c", 3], ["d", 4]];
ientries.partition(x, v => v % 2 == 0).map(x => [...x]);
// → [ [ [ "b", 2 ], [ "d", 4 ] ], [ [ "a", 1 ], [ "c", 3 ] ] ]

var x = [["a", 1], ["b", 2], ["c", 3], ["d", 4], ["e", 5]];
ientries.partition(x, v => v % 2 == 1).map(x => [...x]);
// → [ [ [ "a", 1 ], [ "c", 3 ], [ "e", 5 ] ], [ [ "b", 2 ], [ "d", 4 ] ] ]


References

Clone this wiki locally