Skip to content

Instantly share code, notes, and snippets.

@ilazarte
Created August 13, 2014 15:25
Show Gist options
  • Save ilazarte/b2722a9e5eb285f559cb to your computer and use it in GitHub Desktop.
Save ilazarte/b2722a9e5eb285f559cb to your computer and use it in GitHub Desktop.
(defn partition-every
"adapted from clojure core version of partition-by
partition a collection every time a predicate returns true
=> (partition-every keyword? [:a 1 2 3 :b :c 4 5 6 7 :d :e])
((:a 1 2 3) (:b) (:c 4 5 6 7) (:d) (:e))"
[pred coll]
(lazy-seq
(when-let [s (seq coll)]
(let [fst (first s)
run (cons fst (take-while (complement pred) (next s)))]
(cons run (partition-every pred (seq (drop (count run) s))))))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment