Created
August 16, 2013 15:40
-
-
Save fdserr/6250977 to your computer and use it in GitHub Desktop.
Extract / remove once / remove one instance (no split-with mangling, side-effecting okay):
(extract-one-with [12 2 3 4 5 6] #(when (odd? %) (* 2 %)) -> [[6] [12 2 4 5 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-one-with | |
"=> (extract-one-with [12 2 3 4 5 6] #(when (odd? %) (* 2 %))) | |
[[6] [12 2 4 5 6]]" | |
([coll fun] | |
(extract-one-with coll fun [] [])) | |
([coll fun applied not-applied] | |
(if-not (seq coll) | |
[applied not-applied] | |
(if-let | |
[x (fun (first coll))] | |
(recur (empty coll) fun (conj applied x) (vec (concat not-applied (rest coll)))) | |
(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