With specter:

(map-indexed (fn [i name] (sp/transform sp/MAP-VALS #(nth % i) optij)) 
(:name optij))
=> ({:name :tom, :age 1, :tone :do} {:name :dick, :age 2, :tone :re} {:name 
:harry, :age 3, :tone :mi})

Without:

(map-indexed (fn [i name] (into {} (map (fn [[k v]] [k (nth v i)]) optij))) 
(:name optij))
=> ({:name :tom, :age 1, :tone :do} {:name :dick, :age 2, :tone :re} {:name 
:harry, :age 3, :tone :mi})

If I didn't know which key was the canonical full list, ie some could be 
shorter and in that case I wanted nil:

(map (fn [i] (sp/transform sp/MAP-VALS #(nth % i) optij)) (range 0 (inc 
(apply max (map (comp count vals) optij)))))
=> ({:name :tom, :age 1, :tone :do} {:name :dick, :age 2, :tone :re} {:name 
:harry, :age 3, :tone :mi})


On Tuesday, January 10, 2017 at 7:27:24 AM UTC-8, hiskennyness wrote:
>
> Whenever I code something like this I get the feeling a clojure guru could 
> do it more simply, and I love learning new tricks.
>
> Here is my simple (real-world, btw) problem and solution. Is there a 
> better way?
>
> ;; Problem: given optimized* json maps (* to avoid duplicating keys):
> (def optij {:name [:tom :dick :harry]
>            :age [1 2 3]
>            :tone [:do :re :mi]})
>
> ;; ... produce normal repetitious maps
> (comment
>   [{:name :tom, :age 1, :tone :do}
>    {:name :dick, :age 2, :tone :re}
>    {:name :harry, :age 3, :tone :mi}])
>
> ;; goal #1: pivot so I can use zipmap
> (comment
>   ((:tom 1 :do) (:dick 2 :re) (:harry 3 :mi)))
>
> ;; my goal #1 approach (improvements welcome):
> (apply (partial map (fn [& vs] vs))
>        (vals optij))
>
> (apply (partial map vector)
>        (vals optij))
>
> ;; my overall approach (improvements welcome):
> (let [ks (keys optij)
>       vs-pivoted (apply (partial map vector)
>                         (vals optij))]
>   (vec (for [attributes vs-pivoted]
>          (zipmap ks attributes))))
>
>
> Final fun question: is the original idea of compressing JSON this way 
> commonplace? I thought everyone was just saying to hell with the 
> duplication and taking the convenience of self-defining objects, but 
> apparently one source is not.
>

-- 
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