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?
You can use as->
:
(let [x {:a 1 :b 2}]
(as-> x it
(assoc it :a 20)
(assoc it :b (:a it))))
In addition to akond's comment, note that using as->
can get quite confusing quickly. I recommend either extracting a top level function for these cases, or trying to use as->
in ->
only:
(-> something
(process-something)
(as-> $ (do-something $ (very-complicated $)))
(finish-processing))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With