Re: partition-distinct
On 29 March 2012 21:41, Cedric Greevey cgree...@gmail.com wrote: On Thu, Mar 29, 2012 at 4:18 PM, David Jagoe davidja...@gmail.com wrote: Given a sequence like this: [1 2 1 2 1 1 2 1 2 2 2] partition it to get this: [(1 2) (1 2) (1) (1 2) (1 2) (2) (2)] ! (defn partition-distinct [s] (lazy-seq (loop [seen #{} s (seq s) part []] (if s (let [[f r] s] (if (seen f) (cons (seq part) (partition-distinct s)) (recur (conj seen f) r (conj part f (if-let [part (seq part)] (list part)) Thanks! I had a nasty feeling that it could be done in a one-liner using partition-by or something similar. But of course you need to be able to look at previous elements... -- 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
Re: partition-distinct
Here's a version with reduce. It returns your elements as sets, but you could easily (map seq ,,,) if you really need lists. user= (reduce (fn [r e] (if ((last r) e) (conj r #{e}) (update-in r [(dec (count r))] conj e))) [#{}] [1 2 2 3 4 4 1 6]) [#{1 2} #{2 3 4} #{1 4 6}] Cheers, Jay On Mar 30, 2012, at 4:29 AM, David Jagoe wrote: On 29 March 2012 21:41, Cedric Greevey cgree...@gmail.com wrote: On Thu, Mar 29, 2012 at 4:18 PM, David Jagoe davidja...@gmail.com wrote: Given a sequence like this: [1 2 1 2 1 1 2 1 2 2 2] partition it to get this: [(1 2) (1 2) (1) (1 2) (1 2) (2) (2)] ! (defn partition-distinct [s] (lazy-seq (loop [seen #{} s (seq s) part []] (if s (let [[f r] s] (if (seen f) (cons (seq part) (partition-distinct s)) (recur (conj seen f) r (conj part f (if-let [part (seq part)] (list part)) Thanks! I had a nasty feeling that it could be done in a one-liner using partition-by or something similar. But of course you need to be able to look at previous elements... -- 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 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
Re: partition-distinct
David Jagoe davidja...@gmail.com writes: Hi David, Thanks! I had a nasty feeling that it could be done in a one-liner using partition-by or something similar. But of course you need to be able to look at previous elements... Here's a slightly shorter variant. --8---cut here---start-8--- (defn partition-distinct [c] (reduce #(if (.contains (last %1) %2) (conj %1 [%2]) (conj (subvec %1 0 (dec (count %1))) (conj (last %1) %2))) [[]] (vec c))) --8---cut here---end---8--- The difference is that it'll always return a vector of vectors, and of course the .contains-check is linear for vectors, so it's quadratic to the size of `c` in total. If you don't care about the order of elements in each partition, then you can just go with a vector of sets and have the same performance as Cedric's recursive solution. --8---cut here---start-8--- (defn partition-distinct [c] (reduce #(if ((last %1) %2) (conj %1 #{%2}) (conj (subvec %1 0 (dec (count %1))) (conj (last %1) %2))) [#{}] (vec c))) --8---cut here---end---8--- Bye, Tassilo -- 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
partition-distinct
Hi all, I'm sure I'm missing a really simple way of doing this! Given a sequence like this: [1 2 1 2 1 1 2 1 2 2 2] partition it to get this: [(1 2) (1 2) (1) (1 2) (1 2) (2) (2)] I've been trying to write something generic like partition-by because the values are maps. Also I want to be able to deal with more than 2 distinct values. Thanks! -- David Jagoe davidja...@gmail.com +447535268218 -- 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
Re: partition-distinct
On Thu, Mar 29, 2012 at 4:18 PM, David Jagoe davidja...@gmail.com wrote: Hi all, I'm sure I'm missing a really simple way of doing this! Given a sequence like this: [1 2 1 2 1 1 2 1 2 2 2] partition it to get this: [(1 2) (1 2) (1) (1 2) (1 2) (2) (2)] I've been trying to write something generic like partition-by because the values are maps. Also I want to be able to deal with more than 2 distinct values. Thanks! (defn partition-distinct [s] (lazy-seq (loop [seen #{} s (seq s) part []] (if s (let [[f r] s] (if (seen f) (cons (seq part) (partition-distinct s)) (recur (conj seen f) r (conj part f (if-let [part (seq part)] (list part)) -- 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
Re: partition-distinct
Yeah I don't like that either. Consider (comp vals (partial group-by identity)). On 2012-03-29 16:18 , David Jagoe davidja...@gmail.com wrote: Hi all, I'm sure I'm missing a really simple way of doing this! Given a sequence like this: [1 2 1 2 1 1 2 1 2 2 2] partition it to get this: [(1 2) (1 2) (1) (1 2) (1 2) (2) (2)] I've been trying to write something generic like partition-by because the values are maps. Also I want to be able to deal with more than 2 distinct values. Thanks! -- David Jagoe davidja...@gmail.com +447535268218 -- 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 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
Re: partition-distinct
Meh. Half-assed (mis)reading. Sorry. -Martin On 2012-03-29 16:23 , Weber, Martin S martin.we...@nist.gov wrote: Yeah I don't like that either. Consider (comp vals (partial group-by identity)). On 2012-03-29 16:18 , David Jagoe davidja...@gmail.com wrote: Hi all, I'm sure I'm missing a really simple way of doing this! Given a sequence like this: [1 2 1 2 1 1 2 1 2 2 2] partition it to get this: [(1 2) (1 2) (1) (1 2) (1 2) (2) (2)] I've been trying to write something generic like partition-by because the values are maps. Also I want to be able to deal with more than 2 distinct values. Thanks! -- David Jagoe davidja...@gmail.com +447535268218 -- 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 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 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