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.
user=> (def users [{:name "James" :age 26} {:name "John" :age 43}])
user=> (assoc-in users [1 :age] 44)
[{:name "James", :age 26} {:name "John", :age 44}]
user=> (assoc-in users [1 :password] "nhoJ")
[{:name "James", :age 26} {:password "nhoJ", :name "John", :age 43}]
;; Also (assoc m 2 {...}) or (conj m {...})
user=> (assoc-in users [2] {:name "Jack" :age 19})
[{:name "James", :age 26} {:name "John", :age 43} {:name "Jack", :age 19}]
;; From http://clojure-examples.appspot.com/clojure.core/assoc-in
Dissociates an entry from a nested associative structure returning a
(defn assoc-in
"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."
{:added "1.0"}
[m [k & ks] v]
(if ks
(assoc m k (assoc-in (get m k) ks v))
(assoc m k v)))
Comments top
No comments for assoc-in. Log in to add a comment.