Changes for clojure.core/assoc!

jafingerhut on Wed, 22 Feb 2012
;; The key concept to understand here is that transients are 
;; not meant to be `bashed in place`; always use the value 
;; returned by either assoc! or other functions that operate
;; on transients.

(defn merge2
  "An example implementation of `merge` using transients."
  [x y]
  (persistent! (reduce
                (fn [res [k v]] (assoc! res k v))
                (transient x)
                y)))

;; Why always use the return value, and not the original?  Because the return
;; value might be a different object than the original.  The implementation
;; of Clojure transients in some cases changes the internal representation
;; of a transient collection (e.g. when it reaches a certain size).  In such
;; cases, if you continue to try modifying the original object, the results
;; will be incorrect.

;; Think of transients like persistent collections in how you write code to
;; update them, except unlike persistent collections, the original collection
;; you passed in should be treated as having an undefined value.  Only the return
;; value is predictable.
ghoseb on Sun, 29 Jan 2012
;; The key concept to understand here is that transients are 
;; not meant to be `bashed in place`; always use the value 
;; returned by either assoc! or other functions that operate
;; on transients. 

(defn merge2
  "An example implementation of `merge` using transients."
  [x y]
  (persistent! (reduce
                (fn [res [k v]] (assoc! res k v))
                (transient x)
                y)))
yasuto on Fri, 10 Jun 2011
user> (def bar (transient {:honda 7 :kagawa 23 :ienaga 14}))
#'user/bar
user> (count bar)
3
user> (assoc! bar :nagatomo 55)
#<TransientArrayMap clojure.lang.PersistentArrayMap$TransientArrayMap@6ee7e2ec>
user> (assoc! bar :miyaichi 34 :hasebe 13)
#<TransientArrayMap clojure.lang.PersistentArrayMap$TransientArrayMap@6ee7e2ec>
user> (count bar)
6
user> (persistent! bar)
{:honda 7, :kagawa 23, :ienaga 14, :nagatomo 55, :miyaichi 34, :hasebe 13}
user>