I think what you want is `partition-between` as implemented by amalloys useful
lib
(https://github.com/amalloy/useful/blob/develop/src/flatland/useful/seq.clj#L224
<https://github.com/amalloy/useful/blob/develop/src/flatland/useful/seq.clj#L224>).
`(partition-between (fn [x y]
(not= (inc x) y))
coll)`
Why this isn’t in the standard library is beyond me tbh,
as `partition-by` is a special case for `partition-between`.
`(def partition-by (partial partition-between not=))`
Maybe adding it to clojure.core should be considered?
Also I’d be intrigued to know the rationale behind `partition-by`.
Cheers, Jan
> On 07 Nov 2014, at 18:56, Alex Hammel <[email protected]> wrote:
>
> Here's my take on the 'add delimiters and split' approach. Bonus `congeal
> function, which chunks collections on any condition you like:
>
> (defn- insert-gaps
> [coll pred gap-symbol]
> (reduce (fn [acc x]
> (if (pred (peek acc) x)
> (conj acc x)
> (conj acc gap-symbol x)))
> [(first coll)]
> (rest coll)))
>
> (defn split-coll
> [coll delimiter]
> (->> coll
> (partition-by (partial = delimiter))
> (remove (partial = (list delimiter)))))
>
> (defn congeal
> [pred coll]
> (let [gap-symbol (gensym)]
> (-> coll
> (insert-gaps pred gap-symbol)
> (split-coll gap-symbol))))
>
> (defn consecutive? [p q]
> (= (inc p) q))
>
> (println (congeal consecutive? [1 3 4 5 7 9 10 11 12]))
> #=> ((1) (3 4 5) (7) (9 10 11 12))
> (println (congeal < [1 2 3 1 2 3 1 2 3]))
> #=> ((1 2 3) (1 2 3) (1 2 3))
> (println (congeal not= [:foo :foo :bar :bar :foo :bar]))
> #=> ((:foo) (:foo :bar) (:bar :foo :bar))
>
>
>
>
> On Fri, Nov 7, 2014 at 4:09 AM, Paweł Rozynek <[email protected]
> <mailto:[email protected]>> wrote:
> (def data '(1 3 4 5 7 9 10 11 12))
> (map #(map last %) (partition-by #(apply - %) (map-indexed vector data))) =>
> ((1) (3 4 5) (7) (9 10 11 12))
>
> regards
> PR
>
>
> --
> You received this message because you are subscribed to the Google
> Groups "Clojure" group.
> To post to this group, send email to [email protected]
> <mailto:[email protected]>
> Note that posts from new members are moderated - please be patient with your
> first post.
> To unsubscribe from this group, send email to
> [email protected]
> <mailto:clojure%[email protected]>
> For more options, visit this group at
> http://groups.google.com/group/clojure?hl=en
> <http://groups.google.com/group/clojure?hl=en>
> ---
> You received this message because you are subscribed to the Google Groups
> "Clojure" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to [email protected]
> <mailto:[email protected]>.
> For more options, visit https://groups.google.com/d/optout
> <https://groups.google.com/d/optout>.
>
>
> --
> You received this message because you are subscribed to the Google
> Groups "Clojure" group.
> To post to this group, send email to [email protected]
> Note that posts from new members are moderated - please be patient with your
> first post.
> To unsubscribe from this group, send email to
> [email protected]
> For more options, visit this group at
> http://groups.google.com/group/clojure?hl=en
> <http://groups.google.com/group/clojure?hl=en>
> ---
> You received this message because you are subscribed to the Google Groups
> "Clojure" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to [email protected]
> <mailto:[email protected]>.
> For more options, visit https://groups.google.com/d/optout
> <https://groups.google.com/d/optout>.
--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to [email protected]
Note that posts from new members are moderated - please be patient with your
first post.
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
---
You received this message because you are subscribed to the Google Groups
"Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
For more options, visit https://groups.google.com/d/optout.