Daniel Glauser
unread,Jul 28, 2010, 8:02:18 PM7/28/10Sign in to reply to author
Sign in to forward
You do not have permission to delete messages in this group
Either email addresses are anonymous for this group or you need the view member email addresses permission to view the original message
to Clojure
Hello folks,
I'm working on some sample code and I have a feeling that there is an
easier/more succinct way to code this. Any help or RTFM with a link
is appreciated.
Given:
(def cookbook {:Coffee {:coffee 3, :sugar 1, :cream 1},
:Decaf-Coffee {:decaf 3, :sugar 1, :cream 1},
:Caffe-Late {:espresso 2, :steamed-milk 1},
:Caffe-Americano {:espresso 3},
:Caffe-Moca {:espresso 1, :coco 1, :steamed-milk
1, :cream 1},
:Cappuccino {:espresso 2, :steamed-milk 1, :foamed-milk
1} })
(def cost {:coffee 0.75,
:decaf 0.75,
:sugar 0.25,
:cream 0.25,
:steamed-milk 0.35,
:foamed-milk 0.35,
:espresso 1.00,
:cocoa 0.90,
:whipped-cream 1.00 })
(def menu {:Coffee 1,
:Decaf-Coffee 2,
:Caffe-Late 3,
:Caffe-Americano 4,
:Caffe-Moca 5,
:Cappuccino 6 })
I'm trying to write a function to print out the menu listing the cost
of each drink. It works (sort of) but I keep thinking there is an
easier way.
(defn print-menu [menu]
(do
(println "Menu:")
(doseq [[drink number] menu]
(println (str number ", " (drink-name drink) ", "
(reduce +
(map (fn [map-entry] (* (cost (key map-entry) (val map-
entry)))) (cookbook drink))))))))
Specifically this part:
(map (fn [map-entry] (* (cost (key map-entry) (val map-entry))))
(cookbook drink))
Is there a way I can get at the map key and value using destructuring
without knowing what the key is ahead of time?
Thanks,
Daniel