Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
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]
(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
You can’t perform that action at this time.