Clojure 你能给我一些->;的真实例子吗;宏?
它似乎是一个强大的宏,但除了愚蠢的例子之外,我无法将其应用于任何东西。你能给我看看它的真正用途吗 谢谢 比较:Clojure 你能给我一些->;的真实例子吗;宏?,clojure,macros,Clojure,Macros,它似乎是一个强大的宏,但除了愚蠢的例子之外,我无法将其应用于任何东西。你能给我看看它的真正用途吗 谢谢 比较: user> (:baz (:bar (:foo {:foo {:bar {:baz 123}}}))) 123 user> (java.io.BufferedReader. (java.io.FileReader. "foo.txt")) #<BufferedReader java.io.BufferedReader@6e1f8f> user> (vec
user> (:baz (:bar (:foo {:foo {:bar {:baz 123}}})))
123
user> (java.io.BufferedReader. (java.io.FileReader. "foo.txt"))
#<BufferedReader java.io.BufferedReader@6e1f8f>
user> (vec (reverse (.split (.replaceAll (.toLowerCase "FOO,BAR,BAZ") "b" "x") ",")))
["xaz" "xar" "foo"]
我一直觉得这个例子给人留下深刻印象:
user=> (import '(java.net URL) '(java.util.zip ZipInputStream))
user=> (-> "http://clojure.googlecode.com/files/clojure_20081217.zip"
URL. .openStream ZipInputStream. .getNextEntry bean :name)
正如布赖恩所说,与其说它“有用”,不如说它“与众不同”。我发现对于所有java互操作来说,这种形式的“从X开始”,然后执行Y和Z。。。比X的Z到Y更可读
基本上,您有4种选择:
; imperative style named steps:
(let [X something
b (Y X)
c (Z b)] c)
; nested calls
(Z (Y X))
; threaded calls
(-> X Y Z)
; functional composition
((comp Z Y) X)
我发现->非常适合java互操作,但在其他地方不要使用它
(defn search-tickets-for [term]
(-> term search zip-soup first :content
((partial filter #(= :body (:tag %)))) first :content
((partial filter #(= :div (:tag %))))
((partial filter #(= "content" ((comp :id :attrs) %))))
((partial map :content)) first ((partial map :content))
((partial map first)) ((partial filter #(= :ul (:tag %)))) first :content
((partial map :content))
((partial map first))
((partial mapcat :content))
((partial filter #(= :h4 (:tag %))))
((partial mapcat :content))
((partial filter #(= :a (:tag %))))
((partial mapcat :content))))
来自#clojure的clojurebot使用此搜索assembla票证
; imperative style named steps:
(let [X something
b (Y X)
c (Z b)] c)
; nested calls
(Z (Y X))
; threaded calls
(-> X Y Z)
; functional composition
((comp Z Y) X)
(defn search-tickets-for [term]
(-> term search zip-soup first :content
((partial filter #(= :body (:tag %)))) first :content
((partial filter #(= :div (:tag %))))
((partial filter #(= "content" ((comp :id :attrs) %))))
((partial map :content)) first ((partial map :content))
((partial map first)) ((partial filter #(= :ul (:tag %)))) first :content
((partial map :content))
((partial map first))
((partial mapcat :content))
((partial filter #(= :h4 (:tag %))))
((partial mapcat :content))
((partial filter #(= :a (:tag %))))
((partial mapcat :content))))