Created
June 2, 2022 18:54
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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