Skip to content

Instantly share code, notes, and snippets.

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