Clojure:“线程优先”宏->和“线程最后”宏->>
我在4clojure.com工作,我的解决方案如下:Clojure:“线程优先”宏->和“线程最后”宏->>,clojure,Clojure,我在4clojure.com工作,我的解决方案如下: (defn FPS [s] (->> (map read-string (re-seq #"[0-9]+" s)) (filter #(= (Math/sqrt %) (Math/floor (Math/sqrt %)))) (interpose ",") (apply str))) 它工作得很好。但是如果我使用线程优先宏-> (defn FPS [s] (-> (map r
(defn FPS [s]
(->>
(map read-string (re-seq #"[0-9]+" s))
(filter #(= (Math/sqrt %) (Math/floor (Math/sqrt %))))
(interpose ",")
(apply str)))
它工作得很好。但是如果我使用线程优先宏->
(defn FPS [s]
(->
(map read-string (re-seq #"[0-9]+" s))
(filter #(= (Math/sqrt %) (Math/floor (Math/sqrt %))))
(interpose ",")
(apply str)))
它返回:ClassCastException clojure.lang.LazySeq不能强制转换为clojure.lang.IFn clojure.core/apply core.clj:617
在这个问题中,为什么->>不能被->替换?Thread last macro->>将每个for作为下一个表单的最后一个元素插入。线程第一个宏->将其作为第二个元素插入
那么这个,
(->> a
(b 1)
(c 2))
翻译为:C2B1A,而
转换为:c b a 1 2。线程最后一个宏->>将每个for作为下一个表单的最后一个元素插入。线程第一个宏->将其作为第二个元素插入
那么这个,
(->> a
(b 1)
(c 2))
翻译为:C2B1A,而
在Clojure REPL中翻译为:c b a 1 2.:
user=> (doc ->)
-------------------------
clojure.core/->
([x & forms])
Macro
Threads the expr through the forms. Inserts x as the
second item in the first form, making a list of it if it is not a
list already. If there are more forms, inserts the first form as the
user=> (doc ->>)
-------------------------
clojure.core/->>
([x & forms])
Macro
Threads the expr through the forms. Inserts x as the
last item in the first form, making a list of it if it is not a
list already. If there are more forms, inserts the first form as the
last item in second form, etc.
filter函数期望第一个参数是函数,而不是序列,使用S->不能满足其要求
这就是为什么在代码中无法将clojure.lang.LazySeq转换为clojure.lang.IFn异常的原因。在clojure REPL中:
user=> (doc ->)
-------------------------
clojure.core/->
([x & forms])
Macro
Threads the expr through the forms. Inserts x as the
second item in the first form, making a list of it if it is not a
list already. If there are more forms, inserts the first form as the
user=> (doc ->>)
-------------------------
clojure.core/->>
([x & forms])
Macro
Threads the expr through the forms. Inserts x as the
last item in the first form, making a list of it if it is not a
list already. If there are more forms, inserts the first form as the
last item in second form, etc.
filter函数期望第一个参数是函数,而不是序列,使用S->不能满足其要求
这就是为什么在代码中无法将clojure.lang.LazySeq转换为clojure.lang.IFn异常的原因。->在第二个位置插入,而不是第一个。->在第二个位置插入,而不是第一个位置插入。