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}
>
(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,
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