-
Notifications
You must be signed in to change notification settings - Fork 0
partition
Subhajit Sahu edited this page Dec 28, 2022
·
15 revisions
Segregate values by test result.
Alternatives: partition, partitionAs.
function partition(x, ft)
// x: lists
// ft: test function (v, k, x)
const xlists = require('extra-lists');
var x = [['a', 'b', 'c', 'd'], [1, 2, 3, 4]];
xlists.partition(x, v => v % 2 == 0).map(x => [...x]);
// → [ [ [ 'b', 'd' ], [ 2, 4 ] ], [ [ 'a', 'c' ], [ 1, 3 ] ] ]
var x = [['a', 'b', 'c', 'd', 'e'], [1, 2, 3, 4, 5]];
xlists.partition(x, v => v % 2 == 1).map(x => [...x]);
// → [ [ [ 'a', 'c', 'e' ], [ 1, 3, 5 ] ], [ [ 'b', 'd' ], [ 2, 4 ] ] ]