I have a list of maps
( {:path "first" :size "1 gb"}
{:path "second" :size "500 mb"}
...)
and another list of maps
( {:path "first" :size "1 gb" :date "1"}
{:path "second" :size "500 mb" :date "1"}
{:path "first" :size "0.9 gb" :date "2"}...
{:path "second" :size "400 mb" :date "2"}...
...)
I want to get the first list of maps transformed to something like
( {:path "first" :sizeon1 "1 gb" :sizeon2 "0.9 gb"...}
{:path "second" :sizeon1 "500 mb" :sizeon2 "400 mb"...}
....)
I am a Clojure noob and having a hard time doing this. Can you please help me out?
what would i do, is to rethink the resulting data structure:
I don't know about how would you potentially use the resulting collection, but naming keys :sizeonX
, especially when there is potentially variable amount of registered dates or maybe some of them are missing (like for example if you have dates 1
and 3
for first path, and 1
2
3
5
for the second one) leads to a mess of unpredictably named keys in resulting maps, which would make it way more difficult when it comes to retrieving these keys.
to me it looks like that it would be better to use this structure:
{:path "first" :sizes {"1" "500" "2" "1g" "10" "222"}}
so this sizes map is easily iterated and processed.
that is how would i do that:
(def data '({:path "first" :size "1 gb" :date "1"}
{:path "first" :size "0.9 gb" :date "3"}
{:path "second" :size "500 mb" :date "1"}
{:path "second" :size "700 mb" :date "2"}
{:path "second" :size "400 mb" :date "3"}
{:path "second" :size "900 mb" :date "5"}))
(map (fn [[k v]] {:path k
:sizes (into {} (map (juxt :date :size) v))})
(group-by :path data))
;; ({:path "first", :sizes {"1" "1 gb", "3" "0.9 gb"}}
;; {:path "second", :sizes {"1" "500 mb",
;; "2" "700 mb",
;; "3" "400 mb",
;; "5" "900 mb"}})
update
but as you still need the structure from the question, i would do it like this:
(map (fn [[k v]]
(into {:path k}
(map #(vector (keyword (str "sizeon" (:date %)))
(:size %))
v)))
(group-by :path data))
;;({:path "first", :sizeon1 "1 gb", :sizeon3 "0.9 gb"}
;; {:path "second",
;; :sizeon1 "500 mb", :sizeon2 "700 mb",
;; :sizeon3 "400 mb", :sizeon5 "900 mb"})
which is basically similar to @superkonduktr variant.