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 7 9 11 14 15]) ;=> ([1] [2] [3] [4 6] [7 9 11 14] [15]) On Friday, November 7, 2014 at 7:21:39 PM UTC+1, Jan-Paul Bultmann wrote: > > 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 > ). > > `(partition-between (fn [x y] > (not= (inc x) y)) > coll)` > > Why this isn’t in the standard library is beyond me tbh, > as `partition-by` is a special case for `partition-between`. > > `(def partition-by (partial partition-between not=))` > > Maybe adding it to clojure.core should be considered? > Also I’d be intrigued to know the rationale behind `partition-by`. > > Cheers, Jan > > On 07 Nov 2014, at 18:56, Alex Hammel <[email protected] <javascript:>> > wrote: > > 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] > <javascript:>> 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] >> <javascript:> >> 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] <javascript:> >> 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] <javascript:>. >> 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] <javascript:> > 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] <javascript:> > 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] <javascript:>. > 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.
