我有一些数据如下
;authors
(def china {:name "China Miéville", :birth-year 1972})
(def octavia {:name "Octavia E. Butler"
:birth-year 1947
:death-year 2006})
(def friedman {:name "Daniel Friedman" :birth-year 1944})
(def felleisen {:name "Matthias Felleisen"})
;books
(def cities {:title "The City and the City" :authors #{china}})
(def wild-seed {:title "Wild Seed", :authors #{octavia}})
(def embassytown {:title "Embassytown", :authors #{china}})
(def little-schemer {:title "The Little Schemer"
:authors #{friedman, felleisen}})
(def books [cities, wild-seed, embassytown, little-schemer])
所以,如果我想检查给定的书是否有作者,我会使用该函数
(defn has-author? [book author]
(contains? (:authors book) author)
)
但是我想获取特定作者的书籍,如何通过过滤器获取它?我尝试过:
(defn books-by-author [author books]
(filter has-author? (books author))
)
最佳答案
更改 books-by-author
中的过滤器
,以传递根据 has-author?
定义的谓词以及图书集合,其中每本书都由谓词引用:
(filter #(has-author? % author) books)
关于Clojure:使用多个参数的函数进行过滤,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27585360/