cursive
cursive copied to clipboard
Destructuring with as-> gives unresolved symbol warnings
as-> uses let to assign values to symbols.
(as-> something x
(do-stuff x 1 2)
(do-something-else 3 x))
translates to:
(let [x something
x (do-stuff x 1 2)]
(do-something-else 3 x))
That means you can also do destructuring assignment, as long as all the expressions return a similar structure:
(as-> something {:keys [a b c] :as x}
(assoc x :a (* a b))
(do-something-fantastic b c x))
translating to:
(let [{:keys [a b c] :as x} something
{:keys [a b c] :as x} (assoc x :a (*a b))]
(do-something-fantastic b c x))
This isn't necessarily code I like much, but I've found this littered around our codebase, and I get unresolved warnings where they are used.
Would love to see this added. It can be useful for grabbing some values from the threaded object:
(-> {:a 1}
(assoc :b 2)
(update :a inc)
(as-> {:keys [a b] :as m} (assoc m :c (+ a b)))
...
)