Skip to content

Instantly share code, notes, and snippets.

@martinklepsch
Last active August 29, 2015 14:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save martinklepsch/f44829fb12ddd1cb822a to your computer and use it in GitHub Desktop.
Save martinklepsch/f44829fb12ddd1cb822a to your computer and use it in GitHub Desktop.
(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)])))
@martinklepsch
Copy link
Author

;; (map (fn [c]
;;        (reduce (fn [m]
;;               (assoc-in c [(key m)] ((:campaign_id c) (val m))))
;;             data-to-merge))
;;        campaigns)))

@joegallo
Copy link

(group-by (comp :cid second) 
          (for [[k vs] input 
                v vs] 
               [k v]))

;; is a very decent start

@martinklepsch
Copy link
Author

(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