Great stuff Ben. Thanks. ray
On Saturday, December 15, 2012 10:54:42 PM UTC+1, Benjamin R. Haskell wrote: > > On Sat, 15 Dec 2012, mond wrote: > > > Thanks for picking up the cudgels Ben! > > Ha. It's nice to have reached a point where I feel at-all confident > with any of this... happy to help. > > > > To be honest I am struggling to repeat your advice in the REPL. In > > any case, I decided to change the data structures in line with your > > advice and put the IDs into maps rather than the records. > > > > (defrecord Item [name product quantity purchased]) > > > > (defrecord Product [name description prices]) > > > > (defrecord Price [price tax currency]) > > > > (def items [ {:id 1 :item (->Item "Brogues" "P-123" 1 true)} > > {:id 2 :item (->Item "Underpants" "P-345" 2 false)} > > {:id 3 :item (->Item "Shirt" "P-678" 1 true)} ]) > > > > (def carts [ (->Cart "Birthday" (first items)) > > (->Cart "Xmas" (rest items)) ]) > > > > (def products [ {:id "P-1231" :product (->Product "Table" "Coffee Table" > (->Price 375 21 "EURO"))} > > {:id "P-3451" :product (->Product "Chairs" "Set of > Four(4) chairs" (->Price 200 21 "EURO"))} > > {:id "P-123" :product (->Product "Brogues" "Men's > Leather Slip on Brogues" (->Price 93.00 21 "EURO"))} > > {:id "P-345" :product (->Product "Underpants" "CK Y > Fronts" (->Price 23.50 21 "EURO"))} > > {:id "P-678" :product (->Product "Shirt" "Slim Fit White > Vest Shirt" (->Price 45.99 21 "EURO"))} > > {:id "P-6781" :product (->Product "TableCloth" "Classic > red and white checks 2m x 2m" (->Price 17.99 21 "EURO"))} ]) > > > > Do you think the zipmap is still the way to go (to resolve the > > 'foreign key' or could there be an easier way? I guess the fact that > > only items have to zipmapped is one advantage. > > It seems like items and products should still have an :id property, so I > don't think you need to detach the ID from its entity. The zipmap'ed > version is useful as an index, not really as its own structure. So, it > ends up as just a single map (not an array of maps) with ID's as keys, > and the corresponding entity as the value: > > > (defrecord Item [id name product quantity purchased]) > > (defrecord Product [id name description prices]) > > (defrecord Price [price tax currency]) > > (def items [ (->Item 1 "Brogues" "P-123" 1 true) > (->Item 2 "Underpants" "P-345" 2 false) > (->Item 3 "Shirt" "P-678" 1 true) ]) > > (def products [ (->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-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-6781" "TableCloth" "Classic red and white > checks 2m x 2m" (->Price 17.99 21 "EURO")) ]) > > ; two useful indexes(/indices?): > > (def prod->item > "An index from Product ID to an Item" > (zipmap (map :product items) items)) > > (def id->product > "An index from Product ID to a Product" > (zipmap (map :id products) products)) > > ; Then you can use that to get the products joined with their items: > > (defn product-with-item > "Find the Item info and merge it into the product, without overwriting > :id" > [product] > (merge-with (fn [a b] a) product (prod->item (:id product)))) > > ; example usage: > > (product-with-item (id->product "P-345")) > ;=> #user.Product{:id 2, > ; :name "Underpants", > ; :description "CK Y Fronts", > ; :prices #user.Price{:price 23.5, :tax 21, :currency > "EURO"}, > ; :purchased false, > ; :quantity 2, > ; :product "P-345"} > > (def joined-products (map product-with-item products)) > ;=> A list of all the products joined with their items > > -- > Best, > Ben -- 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