Created
February 3, 2011 02:58
-
-
Save brehaut/808961 to your computer and use it in GitHub Desktop.
a scruffy implementation of a function to pivot on a predicate
This file contains 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 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