Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@brehaut
Created February 3, 2011 02:58
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 brehaut/808961 to your computer and use it in GitHub Desktop.
Save brehaut/808961 to your computer and use it in GitHub Desktop.
a scruffy implementation of a function to pivot on a predicate
(defn pivot-on
"This is about the worst way you could ever write this"
[p coll]
(first (reduce (fn [[[l r] pivoted] v]
(if pivoted
[[l (conj r v)] true]
(if (p v)
[[l r] true]
[[(conj l v) r] false])))
[[[] []] false]
coll)))
(defn pivot-on-2
[p coll]
(loop [[h & r] coll
left []]
(if (p h)
[left r]
(recur r (conj left h)))))
;; amalloy's version
(defn pivot-with
[pred seq]
(let [seq (drop-while pred seq)]
(take-nth 2 (partition-by (comp boolean pred) seq))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment