访问Clojure的线程优先宏参数

Moh*_*nah 7 functional-programming clojure

我想知道是否有一种方法可以在Clojure执行时访问线程优先宏的参数值.例如:

(def x {:a 1 :b 2})
(-> x
    (assoc :a 20) ;; I want the value of x after this step
    (assoc :b (:a x))) ;; {:a 20, :b 1}
Run Code Online (Sandbox Code Playgroud)

我注意到这有效:

(-> x 
    (assoc :a 20) 
    ((fn [x] (assoc x :b (:a x))))) ;; {:a 20, :b 20}
Run Code Online (Sandbox Code Playgroud)

但还有其他方法吗?

ako*_*ond 10

你可以使用as->:

(let [x {:a 1 :b 2}]
    (as-> x it
        (assoc it :a 20)                                             
        (assoc it :b (:a it)))) 
Run Code Online (Sandbox Code Playgroud)