Clojure partition-at
While working with some code, I noticed what appears to be a missing function. I wanted to use something like partition-by, which splits the collection each time the passed function returns a new value. Except I wanted it to split whenever the passed function returns true, instead of when the return value changes. Anyways, here it is:
(defn partition-at
"Like partition-by but will start a new run when f returns true"
[f coll]
(lazy-seq
(when-let [s (seq coll)]
(let [run (cons (first s) (take-while #(not (f %)) (rest s)))]
(cons run (partition-at f (drop (count run) s)))))))
0 Comments:
Post a Comment
<< Home