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, Jun 17, 2014 at 7:24 AM, Shoeb Bhinderwala <shua...@gmail.com>
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}
>              {:id 3 :val 30}
>              {:id 5 :val 50}
>              {:id 6 :val 60}])
>
> I want to create a result zs which is the union of xs and ys, except that
> if the :id occurs in BOTH then I want to multiply the corresponding :val.
>
> The result zs should be:
>
> [{:id 1 :val 10}
>   {:id 2 :val 400}  <-- :id 2 occurs in both collections, so multiply the
> :val fields
>   {:id 3 :val 900}  <-- :id 3 occurs in both collections, so multiply the
> :val fields
>  {:id 4 :val 40}
>  {:id 5 :val 50}
>  {:id 6 :val 60}]
>
> Thanks in advance,
> Shoeb
>
>  --
> You received this message because you are subscribed to the Google
> Groups "Clojure" group.
> To post to this group, send email to clojure@googlegroups.com
> Note that posts from new members are moderated - please be patient with
> your first post.
> To unsubscribe from this group, send email to
> clojure+unsubscr...@googlegroups.com
> For more options, visit this group at
> http://groups.google.com/group/clojure?hl=en
> ---
> You received this message because you are subscribed to the Google Groups
> "Clojure" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to clojure+unsubscr...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.
>

-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
--- 
You received this message because you are subscribed to the Google Groups 
"Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to clojure+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to