assoc

known as "associate"
functionsince v0.0-927 clojure.core/assocEdit
(assoc coll k v)
(assoc coll k v & kvs)

Details:

assoc(iate)

When applied to a map, returns a new map that contains the mapping of key(s) to val(s).

Has no effect on the map type (hashed/sorted).

When applied to a vector, returns a new vector that contains value v at index k.


Examples:

(def my-map {:foo 1})

(assoc my-map :foo 2)
;;=> {:foo 2}

(assoc my-map :bar 2)
;;=> {:foo 1 :bar 2}

(assoc my-map :a 3 :b 4 :c 5 :d 6)
;;=> {:foo 1 :a 3 :b 4 :c 5 :d 6}

;; you must pass a value for every key
(assoc my-map :foo)
;;=> WARNING: Wrong number of args (2) passed to cljs.core/assoc
(def my-vec [1 2 3])

(assoc my-vec 0 "foo")
;;=> ["foo" 2 3]

(assoc my-vec 3 "foo")
;;=> Error: Index 3 out of bounds  [0,0]

See Also:


Source docstring:
assoc[iate]. When applied to a map, returns a new map of the
same (hashed/sorted) type, that contains the mapping of key(s) to
val(s). When applied to a vector, returns a new vector that
contains val at index. Note - index must be <= (count vector).
Source code @ clojurescript:src/main/cljs/cljs/core.cljs
(defn assoc
  ([coll k v]
   (if (implements? IAssociative coll)
     (-assoc coll k v)
     (if-not (nil? coll)
       (-assoc coll k v)
       (array-map k v))))
  ([coll k v & kvs]
     (let [ret (assoc coll k v)]
       (if kvs
         (recur ret (first kvs) (second kvs) (nnext kvs))
         ret))))