assoc!

clojure.core

  • (assoc! coll key val)
  • (assoc! coll key val & kvs)
Alpha - subject to change.
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.

1 Example top

  • ;; 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.
Log in to add / edit an example.

See Also top

Log in to add a see also.

Plus_12x12 Minus_12x12 Source clojure/core.clj:2950 top

(defn assoc!
  "Alpha - subject to change.
  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."
  {:added "1.1"
   :static true}
  ([^clojure.lang.ITransientAssociative coll key val] (.assoc coll key val))
  ([^clojure.lang.ITransientAssociative coll key val & kvs]
   (let [ret (.assoc coll key val)]
     (if kvs
       (recur ret (first kvs) (second kvs) (nnext kvs))
       ret))))
Vars in clojure.core/assoc!:
Used in 0 other vars

Comments top

No comments for assoc!. Log in to add a comment.