Search code examples
clojurefunctional-programming

accessing Clojure's thread-first macro arguments


I was wondering if there was a way to access the arguments value of a thread-first macro in Clojure while it is being executed on. for example:

(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}

It has come to my attention that this works:

(-> x 
    (assoc :a 20) 
    ((fn [x] (assoc x :b (:a x))))) ;; {:a 20, :b 20}

But are there any other ways to do that?


Solution

  • You can use as->:

    (let [x {:a 1 :b 2}]
        (as-> x it
            (assoc it :a 20)                                             
            (assoc it :b (:a it))))