Skip to content

Instantly share code, notes, and snippets.

@mpereira
Last active July 18, 2017 17:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mpereira/c07c4f212f595e0de721849cd2d24910 to your computer and use it in GitHub Desktop.
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.
(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