【发布时间】:2010-06-08 03:31:20
【问题描述】:
我有以下结构向量:
(defstruct #^{:doc "Basic structure for book information."}
book :title :authors :price)
(def #^{:doc "The top ten Amazon best sellers on 16 Mar 2010."}
best-sellers
[(struct book
"The Big Short"
["Michael Lewis"]
15.09)
(struct book
"The Help"
["Kathryn Stockett"]
9.50)
(struct book
"Change Your Prain, Change Your Body"
["Daniel G. Amen M.D."]
14.29)
(struct book
"Food Rules"
["Michael Pollan"]
5.00)
(struct book
"Courage and Consequence"
["Karl Rove"]
16.50)
(struct book
"A Patriot's History of the United States"
["Larry Schweikart","Michael Allen"]
12.00)
(struct book
"The 48 Laws of Power"
["Robert Greene"]
11.00)
(struct book
"The Five Thousand Year Leap"
["W. Cleon Skousen","James Michael Pratt","Carlos L Packard","Evan Frederickson"]
10.97)
(struct book
"Chelsea Chelsea Bang Bang"
["Chelsea Handler"]
14.03)
(struct book
"The Kind Diet"
["Alicia Silverstone","Neal D. Barnard M.D."]
16.00)])
I would like to sum the prices of all the books in the vector. What I have is the following:
(defn get-price
"Same as print-book but handling multiple authors on a single book"
[ {:keys [title authors price]} ]
price)
然后我:
(reduce + (map get-price best-sellers))
有没有一种方法可以在不将“get-price”函数映射到向量上的情况下做到这一点?还是有解决这个问题的惯用方法?
【问题讨论】:
-
你的方法几乎是解决这个问题的惯用方法。
-
defstruct可能会被defrecord/deftype弃用/取代,仅供参考。 groups.google.com/group/clojure/msg/330c230e8dc857a9
标签: clojure