(doc->;)clojure的解释
如果运行(doc->;)clojure的解释,clojure,Clojure,如果运行(doc->),它会显示: clojure.core/-> ([x] [x form] [x form & more]) 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
(doc->)
,它会显示:
clojure.core/->
([x] [x form] [x form & more])
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
second item in second form, etc.
我不明白为什么它在第一种形式中说第二项,在这种情况下第一项是什么
即使您运行(source->)
:
x
作为第一个参数传递给表单
,因此我在这里遗漏了什么,为什么文档调用x
第二项调用每个函数时,前面函数的结果作为列表中的第二项传递。它将放在函数名后面:
(third-function
(second-function
(first-function :initial-value :second-arg-to-first-function)
:second-arg-to-second-function)
:second-arg-to-third-function)
如果我们改为使用threading宏(并标记列表位置),将转换为:
此宏将上一个函数的值置于位置2
有一个非常类似的宏as->
(发音为“thread as”),它为线程值提供了一个明确的名称,并有助于使转换更加清晰:
user> (as-> 4 x ;; x starts as 4
(+ x 4) ;; and it's value in each call is the result of the previous
(- x 2 1)
(* x x 2))
50
表单的第二项是函数的第一个参数 这是一张列表表格
(println "world")
求值语义是进行函数调用,列表的第一项是函数,其余项是参数
不过,宏不会评估它们的参数。我们可以通过引用来抑制自己的评价,看看发生了什么
(def foo '(println "world"))
(first foo) ;=> foo
(second foo) ;=> "world"
如果我们在此表格中插入“hello”作为第二项
(def bar `(~(first foo) "hello" ~@(rest foo)))
bar ;=> (clojure.core/println "hello" "world")
(-> "hello" (println "world"))
;=>hello world
(def foo '(println "world"))
(first foo) ;=> foo
(second foo) ;=> "world"
(def bar `(~(first foo) "hello" ~@(rest foo)))
bar ;=> (clojure.core/println "hello" "world")
(-> "hello" (println "world"))
;=>hello world