Skip to content

Instantly share code, notes, and snippets.

@mpereira mpereira/split_by.clj
Last active Jul 18, 2017

Embed
What would you like to do?
Splits coll by pred. Returns a vector with a vector where (pred item) returns true followed by a vector where (pred item) returns false.
(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
You can’t perform that action at this time.