Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- (defn partition-while
- "Given a coll, partition the coll whenever `(f a b)` returns `false`.
- Example: `(partition-when #(<= 0 (Math/abs (- %1 %2)) 1) [1 2 3 5 6 8 10 10])
- returns `[[1 2 3] [5 6] [8] [10 10]]`
- "
- ([f coll] (partition-while f coll [] []))
- ([f [_ & t :as c] result intermediate]
- (let [[a b & _] c]
- (cond
- (nil? a) result
- (nil? b) (conj result (conj intermediate a))
- (f a b) (recur f t result (conj intermediate a))
- :else (recur f t (conj result (conj intermediate a)) [])))))
Add Comment
Please, Sign In to add comment