Re: better way to group consecutive numbers in a vector?

2016-10-01 Thread Rob Jentzema
Nice, I like that solution. Clean, logical and semantical :) (defn consecutive? [[x y]] (= (inc x) y)) (def nonconsecutive? (complement consecutive?)) (partition-between nonconsecutive? [1 2 3 4 6 7 9 11 14 15]) ;=> ([1 2 3 4] [6 7] [9] [11] [14 15]) (partition-between consecutive? [1 2 3 4 6

Re: better way to group consecutive numbers in a vector?

2014-11-07 Thread vvedee
(let [data [1 3 4 5 7 9 10 11 12] seen (atom (first data))] (partition-by #(if ( (- % @seen) 2) (do (reset! seen %) true) (do (reset! seen %) false)) data)) ((1) (3 4 5) (7) (9 10 11 12)) On Thursday, November 6, 2014 11:22:14 PM

Re: better way to group consecutive numbers in a vector?

2014-11-07 Thread Paweł Rozynek
(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

Re: better way to group consecutive numbers in a vector?

2014-11-07 Thread Alex Hammel
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

Re: better way to group consecutive numbers in a vector?

2014-11-07 Thread Jan-Paul Bultmann
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]

better way to group consecutive numbers in a vector?

2014-11-06 Thread John Gabriele
Hi all, I've got this: `[1 3 4 5 7 9 10 11 12]` and I'd like to turn it into this: `[[1] [3 4 5] [7] [9 10 11 12]]`. That is, I'd like to group consecutive numbers together (the final goal being to produce something like `[1 3-5 7 9-12]`, but that's the easy part). I haven't found an easy way

Re: better way to group consecutive numbers in a vector?

2014-11-06 Thread Ashton Kemerling
Consider using partition-by. I can't type clojure on my phone, but the following link might help. https://clojuredocs.org/clojure.core/partition-by --Ashton Sent from my iPhone On Nov 6, 2014, at 3:22 PM, John Gabriele jmg3...@gmail.com wrote: Hi all, I've got this: `[1 3 4 5 7 9 10 11

Re: better way to group consecutive numbers in a vector?

2014-11-06 Thread Steve Miner
I would try to avoid last and but-last as they work in linear time. How about something like this? (defn congeal-consecutives [coll] (when (seq coll) (let [[_ group res] (reduce (fn [[succ group res] n] (if (== succ n)

Re: better way to group consecutive numbers in a vector?

2014-11-06 Thread john walker
Hi John, As miner mentioned, peek is preferable to last for vectors. Here is an implementation based on the observation that consecutive increasing numbers are equivalent when you subtract their indices. Maybe it is more clever than simple. (ns experimental-clojure.congeal-consecutives) (def

Re: better way to group consecutive numbers in a vector?

2014-11-06 Thread Gordon Stratton
On Thu, Nov 6, 2014 at 10:22 PM, John Gabriele jmg3...@gmail.com wrote: Hi all, I've got this: `[1 3 4 5 7 9 10 11 12]` and I'd like to turn it into this: `[[1] [3 4 5] [7] [9 10 11 12]]`. That is, I'd like to group consecutive numbers together (the final goal being to produce something

Re: better way to group consecutive numbers in a vector?

2014-11-06 Thread blake
I wanted to put the delimiters in one step and then split in a different one, so I did this: (defn delimit[v] (reduce #(if (= (last %) (dec %2)) (conj % %2) (conj % :split %2)) [(first v)] (rest v))) (delimit [1 3 4 5 7 9 10 11 12]) = [1 :split 3 4 5 :split 7

Re: better way to group consecutive numbers in a vector?

2014-11-06 Thread John Gabriele
On Thursday, November 6, 2014 8:48:07 PM UTC-5, blake wrote: I wanted to put the delimiters in one step and then split in a different one, so I did this: (defn delimit[v] (reduce #(if (= (last %) (dec %2)) (conj % %2) (conj % :split %2)) [(first v)]

Re: better way to group consecutive numbers in a vector?

2014-11-06 Thread Ben Wolfson
I don't know if this is *clearer* than the reduce version ... user (def xs [1 3 4 5 7 9 10 11 12 ]) #'user/xs user (defn consecutivizer [] (let [prev (atom nil) last-returned (atom (gensym))] (fn [cur] (when-not (or (nil? @prev)