我在Clojure wikibook上看到以下代码
user=> (filter nil? [:a :b nil nil :a])
(nil nil)
Run Code Online (Sandbox Code Playgroud)
我看到的nil?
是一个谓词.但是,我目前想要转换clojure.contrib.string/substring?
为谓词,这意味着,虽然substring
是一个接受两个参数的函数,但我想将第一个设置为固定.我怎样才能做到这一点?
目前我正在写这样的东西
(filter (fn [x] (clojure.contrib.string/substring? "todo" x))
["todo" "todo" nil "todos"])
Run Code Online (Sandbox Code Playgroud)
有没有更好的办法?
你的代码很好,除了
clojure.contrib.string
已经被吸收clojure.string
,substring?
现在已经被吸收了.请参阅clojure.contrib迁移.
您需要保护nil
样本数据中的s.
#()
如果您愿意,也可以使用阅读器宏,例如使用正则表达式
(filter #(when % (re-find #"todo" %)) ["todo" "todo" nil "todos"])
Run Code Online (Sandbox Code Playgroud)
或Java互操作
(filter #(when % (.contains % "todo")) ["todo" "todo" nil "todos"])
Run Code Online (Sandbox Code Playgroud)
都
;=> ("todo" "todo" "todos")
Run Code Online (Sandbox Code Playgroud)