Last active
August 29, 2015 14:09
-
-
Save martinklepsch/f44829fb12ddd1cb822a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(def input | |
{:tracker [{:cid 1 :foo "b"} {:cid 2 :bar "d"}] | |
:images [{:cid 1 :baz "a"} {:cid 1 :bar "abc"} {:cid 2 :boo "e"}]}) | |
(def desired-output | |
{1 {:tracker [{:foo "b"}] :images [{:baz "a"} {:bar "abc"}]} | |
2 {:tracker [{:bar "d"}] :images [{:boo "e"}]}}) | |
; Solution h/t Paulus Esterhazy | |
(defn mrg [kk input] | |
(reduce | |
(fn [m [k v]] (update-in m k #(conj % v))) | |
{} | |
(for [[table rows] input row rows] | |
[[(kk row) table] (dissoc row kk)]))) |
Author
martinklepsch
commented
Nov 12, 2014
(group-by (comp :cid second)
(for [[k vs] input
v vs]
[k v]))
;; is a very decent start
(defn mrg [input]
(reduce
(fn [m [k v]] (update-in m k #(merge % v)))
{}
(for [[table rows] input row rows] [[(:cid row) table] (dissoc row :cid)])))
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment