a data conversion question
Is there a better way to take this... [[:a [1 2]] [:b [3 4]]] and convert it to this... [[:a 1] [:a 2] [:b 3] [:b 4]] than using a loop like this... (defn doit [id v] (loop [a (first v) r (rest v) result []] (if a (recur (first r) (rest r) (conj result [id a])) result))) ??? Thanks! ,boz -- 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.
Re: a data conversion question
(for[[k v] [[:a [1 2]] [:b [3 4]]] n v] [k n]) -- 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.
Re: a data conversion question
(mapcat (fn [[k coll]] (map vector (repeat k) coll)) [[:a [1 2]] [:b [3 4]]]) = '([:a 1] [:a 2] [:b 3] [:b 4]) On Sunday, June 8, 2014 2:12:19 PM UTC-5, boz wrote: Is there a better way to take this... [[:a [1 2]] [:b [3 4]]] and convert it to this... [[:a 1] [:a 2] [:b 3] [:b 4]] than using a loop like this... (defn doit [id v] (loop [a (first v) r (rest v) result []] (if a (recur (first r) (rest r) (conj result [id a])) result))) ??? Thanks! ,boz -- 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.
Re: a data conversion question
On Jun 8, 2014, at 3:12 PM, boz b...@cox.net wrote: Is there a better way to take this... [[:a [1 2]] [:b [3 4]]] and convert it to this... [[:a 1] [:a 2] [:b 3] [:b 4]] than using a loop like this... (defn doit [id v] (loop [a (first v) r (rest v) result []] (if a (recur (first r) (rest r) (conj result [id a])) result))) ??? Lots of possibilities, and your loop doesn't look to me like it's handling the ids right, but FWIW I might do something like: (defn split-key-pairs [grouped-data] (vec (apply concat (for [[key vals] grouped-data] (for [v vals] [key v]) = (split-key-pairs [[:a [1 2]] [:b [3 4]]]) [[:a 1] [:a 2] [:b 3] [:b 4]] -Lee -- 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.
Re: a data conversion question
If you really want a vector, just wrap my answer with (into [] ...): (into[] (for[[k v] [[:a [1 2]] [:b [3 4]]] n v] [k n])) = [[:a 1] [:a 2] [:b 3] [:b 4]] -- 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.
Re: a data conversion question
Thanks for the replies!! I goofed and only gave part of my loopy solution ... here's the whole thing (defn do-one [id v] (loop [a (first v) r (rest v) result []] (if a (recur (first r) (rest r) (conj result [id a])) result))) (defn do-all [x] (loop [a (first x) b (rest x) result []] (if a (recur (first b) (rest b) (into result (do-one (first a) (flatten (rest a) result))) On Sunday, June 8, 2014 12:12:19 PM UTC-7, boz wrote: Is there a better way to take this... [[:a [1 2]] [:b [3 4]]] and convert it to this... [[:a 1] [:a 2] [:b 3] [:b 4]] than using a loop like this... (defn doit [id v] (loop [a (first v) r (rest v) result []] (if a (recur (first r) (rest r) (conj result [id a])) result))) ??? Thanks! ,boz -- 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.
Re: a data conversion question
Wow! 'for' is a powerful thing!! This is perfect! On Sunday, June 8, 2014 12:32:44 PM UTC-7, François Rey wrote: (for [[k v] [[:a [1 2]] [:b [3 4]]] n v] [k n]) -- 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.
Re: a data conversion question
Yet another way: (reduce #(let [[k [v1 v2]] %2] (vec (concat % [[k v1]] [[k v2]]))) [] [[:a [1 2]] [:b [3 4]]]) On Jun 8, 2014, at 3:43 PM, boz b...@cox.net wrote: Thanks for the replies!! I goofed and only gave part of my loopy solution ... here's the whole thing (defn do-one [id v] (loop [a (first v) r (rest v) result []] (if a (recur (first r) (rest r) (conj result [id a])) result))) (defn do-all [x] (loop [a (first x) b (rest x) result []] (if a (recur (first b) (rest b) (into result (do-one (first a) (flatten (rest a) result))) On Sunday, June 8, 2014 12:12:19 PM UTC-7, boz wrote: Is there a better way to take this... [[:a [1 2]] [:b [3 4]]] and convert it to this... [[:a 1] [:a 2] [:b 3] [:b 4]] than using a loop like this... (defn doit [id v] (loop [a (first v) r (rest v) result []] (if a (recur (first r) (rest r) (conj result [id a])) result))) ??? Thanks! ,boz -- 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. -- 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.