function | since v0.0-927 | clojure.core/assoc | Edit |
(assoc coll k v)
(assoc coll k v & kvs)
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
.
(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]
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).
(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))))