Last active
July 18, 2017 17:50
-
-
Save mpereira/c07c4f212f595e0de721849cd2d24910 to your computer and use it in GitHub Desktop.
Splits coll by pred. Returns a vector with a vector where (pred item) returns true followed by a vector where (pred item) returns false.
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
(defn split-by | |
"Splits coll by pred. Returns a vector with a vector where (pred item) returns | |
true followed by a vector where (pred item) returns false. | |
Example: | |
(split-by pos? [0 1 2 -1 3 -2 4 -3]) | |
=> [[1 2 3 4] [0 -1 -2 -3]]" | |
[pred coll] | |
(reduce (fn [split item] | |
(update split (if (pred item) 0 1) conj item)) | |
[[] []] | |
coll)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment