I wrote a generalized version of this called partition-between, which
you can see at 
https://github.com/flatland/useful/blob/develop/src/useful/seq.clj#L181
if you're interested. Using that as a primitive, your break-on-gaps
function is simple:

user> (partition-between (fn [[a b]] (not= a (dec b))) [1 2 3 5 6 7 8
10 20 21])
([1 2 3] [5 6 7 8] [10] [20 21])

On Sep 28, 11:39 am, qhfgva <qhf...@gmail.com> wrote:
> I've been working on problems from "Programming Challenges" (Skiena)
> to learn clojure.  As part of a problem I developed the following
> routine.  I sort of scare myself how natural thinking in reduce is
> getting, but I was wondering if there is a more clever/idiomatic way
> to solve this problem.
>
> (defn break-on-gaps [minutes]
>   (reduce (fn [acc x]
>             (if (empty? acc)
>               [[x]]
>               (if (= (inc (last (last acc))) x)
>                 (conj (vec (butlast acc))
>                       (conj (last acc) x))
>                 (conj acc [x]))))
>           []
>           minutes))
>
> user=> (break-on-gaps [1 2 3 5 6 7 8 10 20 21])
> [[1 2 3] [5 6 7 8] [10] [20 21]]

-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en

Reply via email to