qhfgva, 2011-09-28 20:39 +0200
> 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]]

Instead of folding from left with reduce 
you might consider the pros and cons of folding from right 
with 

(defn foldr [f f0 s]
  (if (empty? s)
    f0
    (f (first s) (foldr f f0 (rest s)))))

then 

(defn break-on-gaps [minutes]
  (foldr (fn [x acc]
           (if (= (inc x) (ffirst acc))
             (cons (cons x (first acc)) (rest acc))
             (cons (cons x ())          acc)))
         ()
         minutes))

-- 
   Take it Easy          Don't Hurry            Be Happy

                           Thierry

°¨¨¨¨°şo§oş°¨¨¨¨°şo§oş°¨¨¨¨°şo§oş°¨¨¨¨°şo§oş°¨¨¨¨°şo§oş°¨¨¨¨°

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