Created
July 5, 2018 11:24
-
-
Save zipang/84e543dd2f664b45fece9e3a3a0dbcf3 to your computer and use it in GitHub Desktop.
Array prototype partition
This file contains 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
/** | |
* Partition an array on the result of a boolean test | |
* @return {Array[Array]} where the first array contains the element that passed the test | |
*/ | |
Array.prototype.partition = (test) => this.reduce( | |
(accumulator, val) => { | |
accumulator[test(val)?0:1] = val | |
return accumulator | |
})([[], []]) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment