Clojure transients usage

后端 未结 1 866
轻奢々
轻奢々 2020-12-12 00:47

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 [])]
             


        
相关标签:
1条回答
  • 2020-12-12 01:18

    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))))
    
    0 讨论(0)
提交回复
热议问题