Help with writing a function to merge two maps

2014-06-17 Thread Mike Fikes
I want to use merge-with * to solve your problem. To do so, define a couple of helper functions (defn v->m [v] (into (sorted-map) (for [{:keys [id val]} v] [id val]))) (defn m->v [m] (mapv (fn [[k v]] {:id k :val v}) m)) that can convert your structure to and from sorted maps. With these,

Re: Help with writing a function to merge two maps

2014-06-17 Thread Ray Miller
On 17 June 2014 13:24, Shoeb Bhinderwala wrote: > Can someone help me write the following function: > > I have two lists of maps as inputs: > > (def xs [{:id 1 :val 10} > {:id 2 :val 20} > {:id 3 :val 30} > {:id 4 :val 40}]) > > (def ys [{:id 2 :val 20} >

Re: Help with writing a function to merge two maps

2014-06-17 Thread Di Xu
(let [trans (fn [ent] (reduce #(assoc %1 (:id %2) (:val %2)) {} ent))] (for [i (merge-with * (trans xs) (trans ys))] {:id (first i) :val (second i)})) ​you can sort on the result of above expression to get sorted vector.​ Thanks,

Re: Help with writing a function to merge two maps

2014-06-17 Thread Jeb Beich
Here's what I came up with: (require '[clojure.set :as set]) (let [->map-fn (fn [s] (->> s (map (juxt :id :val)) (into {}))) xs-m (->map-fn xs) ys-m (->map-fn ys)] (->> (set/union (keys xs-m) (keys ys-m)) (map (fn [k] {:id k :val (* (get xs-m k 1) (get ys-m k 1))} On Tue

Help with writing a function to merge two maps

2014-06-17 Thread Shoeb Bhinderwala
Can someone help me write the following function: I have two lists of maps as inputs: (def xs [{:id 1 :val 10} {:id 2 :val 20} {:id 3 :val 30} {:id 4 :val 40}]) (def ys [{:id 2 :val 20} {:id 3 :val 30} {:id 5 :val 50}