ClojureDocs

Nav

Namespaces

assoc!

clojure.core

Available since 1.1 (source)
  • (assoc! coll key val)
  • (assoc! coll key val & kvs)
When applied to a transient map, adds mapping of key(s) to
val(s). When applied to a transient vector, sets the val at index.
Note - index must be <= (count vector). Returns coll.
2 Examples
;; 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.  See one example for conj! that contains a detailed
;; example of a wrong result that can occur if you do not use its return value.

;; 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.
(def m (assoc! (transient {})  :x 1 :y 2))

(:x m)
;; 1

(:y m)
;; 2

(count m)
;; 2
See Also

Returns a transient map that doesn't contain a mapping for key(s).

Added by kumarshantanu

Returns a new, transient version of the collection, in constant time.

Added by kumarshantanu

Adds x to the transient collection, and return coll. The 'addition' may happen at different 'place...

Added by ghoseb

Removes the last item from a transient vector. If the collection is empty, throws an exception. Re...

Added by jafingerhut

disj[oin]. Returns a transient set of the same (hashed/sorted) type, that does not contain key(s).

Added by jafingerhut

Returns a new, persistent version of the transient collection, in constant time. The transient col...

Added by jafingerhut
0 Notes
No notes for assoc!