Skip to content

Instantly share code, notes, and snippets.

@henrik1
Created June 2, 2022 18:54
Show Gist options
  • Save henrik1/983cce60d78c9864bd446886248b3030 to your computer and use it in GitHub Desktop.
Save henrik1/983cce60d78c9864bd446886248b3030 to your computer and use it in GitHub Desktop.
Split the values of a given list into two lists, one containing values the predicate function evaluates to truthy, the other one containing the falsy
const bifurcateBy = (predicate, list) =>
list.reduce((acc, val, i) => (
acc[predicate(val, i) ? 0 : 1].push(val), acc),
[[], []]
);
bifurcateBy(val => val > 0, [-1, 2, -3, 4]);
// = [[2, 4], [-1, -3]]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment