On 17 June 2014 13:24, 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}merge
>  {:id 5 :val 50}
>  {:id 6 :val 60}]

Not too different from the other solutions proposed, but how about:

(defn merge-with-multiply [& xs]
  (map (fn [[k v]] {:id k :val v}) (reduce (partial merge-with *) (map
#(into {} (map (juxt :id :val) %)) xs))))

(merge-with-multiply xs ys)
;;=> ({:id 6, :val 60} {:id 5, :val 50} {:id 1, :val 10} {:id 2, :val
400} {:id 3, :val 900} {:id 4, :val 40})

The basic idea is to coerce your input to maps then take advantage of
`merge-with`. Note that this solution assumes that each :id appears at
most once in each of `xs` and `ys`. Here's an alternative solution
without this constraint:

(map (fn [[id vs]] {:id id :val (reduce * (map :val vs))}) (group-by
:id (concat xs ys)))

-- 
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