function | since v0.0-927 | clojure.core/assoc-in | Edit |
(assoc-in m [k & ks] v)
Associates a value in a nested associative structure, where ks
is a sequence
of keys and v
is the new value. Returns a new nested structure.
If any levels do not exist, hash-maps will be created.
(def users [{:name "James" :age 26}
{:name "John" :age 43}])
Update the age of the second (index 1) user:
(assoc-in users [1 :age] 44)
;;=> [{:name "James", :age 26}
;; {:name "John", :age 44}]
Insert the password of the second (index 1) user:
(assoc-in users [1 :password] "nhoJ")
;;=> [{:name "James", :age 26}
;; {:password "nhoJ", :name "John", :age 43}]
Associates a value in a nested associative structure, where ks is a sequence of keys and v is the new value and returns a new nested structure. If any levels do not exist, hash-maps will be created.
(defn assoc-in
[m [k & ks] v]
(if ks
(assoc m k (assoc-in (get m k) ks v))
(assoc m k v)))