Hi, Ray.

When you are doing things that have a relational algebra feel to them 
(joins, etc.), you might want to consider the fns in the clojure.set 
namespace.  E.g.:

;; convert vectors of records to sets, then:
> (clojure.set/join 
      (clojure.set/rename items {:id :iid :name :iname}) ; to prevent 
overwriting keys in the result
      products 
      {:product :id}) ; 'where items.product = products.id'
=>
#{
  {:prices #user.Price{:price 45.99, :tax-rate 21, :currency "EURO"}, 
   :description "Slim Fit White Vest Shirt", 
   :name "Shirt", :id "P-678", 
   :iid 3, 
   :product "P-678", 
   :quantity 1, 
   :purchased true, 
   :iname "Shirt"}
  ...}

It might look even nicer if you just used regular maps with namespaced keys 
instead of records:
(clojure.set/join items products {:item/product :product/id})

Hope that helps,
Leif

On Saturday, December 15, 2012 11:25:59 AM UTC-5, mond wrote:
>
> I have defined these types of records for modelling a simple shopping cart:
>
> ; An item in the cart
> (defrecord Item [id name product quantity purchased])
>
> ; The product that relates to the item in the cart
> (defrecord Product [id name description prices])
>
> ; A price definition
> (defrecord Price [price tax-rate currency])
>
>
> ; Sample items
> (def items [
>              (->Item 1 "Brogues" "P-123" 1 true)
>              (->Item 2 "Underpants" "P-345" 1 false)
>              (->Item 3 "Shirt" "P-678" 1 true)
>              ])
>
> ; Sample products
> (def products [
>                 (->Product "P-123" "Brogues" "Men's Leather Slip on 
> Brogues" (->Price 93.00 21 "EURO"))
>                 (->Product "P-345" "Underpants" "CK Y Fronts" (->Price 
> 23.50 21 "EURO"))
>                 (->Product "P-678" "Shirt" "Slim Fit White Vest Shirt" 
> (->Price 45.99 21 "EURO"))
>                 (->Product "P-1231" "Table" "Coffee Table" (->Price 375 21 
> "EURO"))
>                 (->Product "P-3451" "Chairs" "Set of Four(4) chairs" 
> (->Price 200 21 "EURO"))
>                 (->Product "P-6781" "TableCloth" "Classic red and white 
> checks 2m x 2m" (->Price 17.99 21 "EURO"))
>                 ])
>
> My requirement is to combine data from the two collections such that I can 
> show the detailed product information for any item, for example:
>
>              (->Item 3 "Shirt" "Slim Fit White Vest Shirt" (->Price 45.99 
> 21 "EURO") 1 true)
>
> When I tried to use merge-with union I had an error which surprised me 
> since I thought records are also maps:
>
> user=> (merge-with union items products)
> ClassCastException user.Product cannot be cast to java.util.Map$Entry 
>  clojure.core/key (core.clj:1465)
>
> Although I will fight on, any tips from any of you that have done this 
> before would be greatly helped.
>
> Thanks
>
> ray
>

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

Reply via email to