Skip to content

Instantly share code, notes, and snippets.

@fdserr
Created August 16, 2013 15:40
Show Gist options
  • Save fdserr/6250977 to your computer and use it in GitHub Desktop.
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]]
(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