Guest User

Untitled

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