(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,
Di Xu
2014-06-17 20:24 GMT+08:00 Shoeb Bhinderwala <[email protected]>:
> 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 [email protected]
> Note that posts from new members are moderated - please be patient with
> your first post.
> To unsubscribe from this group, send email to
> [email protected]
> 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 [email protected].
> 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 [email protected]
Note that posts from new members are moderated - please be patient with your
first post.
To unsubscribe from this group, send email to
[email protected]
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 [email protected].
For more options, visit https://groups.google.com/d/optout.