Skip to content

Instantly share code, notes, and snippets.

@zipang
Created July 5, 2018 11:24
Show Gist options
  • Save zipang/84e543dd2f664b45fece9e3a3a0dbcf3 to your computer and use it in GitHub Desktop.
Save zipang/84e543dd2f664b45fece9e3a3a0dbcf3 to your computer and use it in GitHub Desktop.
Array prototype partition
/**
* 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