Created
August 16, 2013 15:17
-
-
Save fdserr/6250815 to your computer and use it in GitHub Desktop.
Kind of split-with, but fn is applied only once, so side-effects are okay :
(extract-all-with [1 2 3 4 5 6] #(when (odd? %) (* 2 %)))
-> [[2 6 10] [2 4 6]]
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 extract-all-with | |
"=> (extract-all-with [1 2 3 4 5 6] #(when (odd? %) (* 2 %))) | |
[[2 6 10] [2 4 6]]" | |
([coll fun] | |
(extract-all-with coll fun (transient []) (transient []))) | |
([coll fun applied not-applied] | |
(if-not (seq coll) | |
[(persistent! applied) (persistent! not-applied)] | |
(if-let | |
[x (fun (first coll))] | |
(recur (rest coll) fun (conj! applied x) not-applied) | |
(recur (rest coll) fun applied (conj! not-applied (first coll))))))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment