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]> 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]
> 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.
>
--
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.