Dev HelpersOpen library
← Back

Partition by predicate

Partition by predicate. A self-contained example using the JavaScript runtime.

Open this example in the interactive library

Source code

const partition = (xs, test) =>
  xs.reduce((out, x) => (out[test(x) ? 0 : 1].push(x), out), [[], []]);
console.log(partition([1, 2, 3, 4], (n) => n % 2 === 0));

Sample output

[ [ 2, 4 ], [ 1, 3 ] ]