I\'m a bit lost with usage of transients in clojure. Any help will be appreciated. The sample code:
(defn test-transient [v]
(let [b (transient [])]
Transients aren't supposed to be bashed in-place like that. Your last example only works due to implementation details which you shouldn't rely on.
The reason why for
doesn't work is that it is lazy and the conj!
calls are never executed, but that is besides the point, as you shouldn't work with transients that way anyway.
You should use conj!
the same way as you would use the "regular" conj
with immutable vectors - by using the return value.
What you are trying to do could be accomplished like this:
(defn test-transient [v]
(let [t (transient [])]
(persistent! (reduce conj! t v))))