Brilliant. Thanks, Shantanu!

On Wednesday, March 19, 2014 12:09:56 AM UTC-4, Shantanu Kumar wrote:
>
> Something like this?
>
> (defn x [1 3 4 5 7 9 10 13])
>
> (reduce (fn [a i] (let [y (last a) z (last y)] (if (and z (= (inc z) i)) 
> (conj (pop a) (conj y i)) (conj a [i])))) [] x)
>
> Shantanu
>
> On Wednesday, 19 March 2014 08:26:43 UTC+5:30, John Gabriele wrote:
>>
>> If you've got a sorted list of numbers, for example:
>>
>>     [1 3 4 5 7 9 10 13]
>>
>> where some are consecutive, how can you pull out the consecutive runs? 
>> That is, either produce
>>
>>     [1 [3 4 5] 7 [9 10] 13]    ; or maybe something like
>>     [[1 7 13] [3 4 5] [9 10]]  ; (the first vec is the elements left over)
>>
>> from the original coll?
>>
>> I can do it in Python in an imperative style, but it's a bit messy. My 
>> hunch is that there's probably a shorter way in Clojure, but I'm not seeing 
>> it.
>>
>> Thanks!
>>
>>

-- 
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
--- 
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 clojure+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to