You're viewing version 1.2.0 of assoc-in. The latest stable version of Clojure Core is 1.3.0.
1.2.0 Arrow_down_16x16

assoc-in

clojure.core

  • (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 and returns a new nested structure.
If any levels do not exist, hash-maps will be created.

2 Examples top

  • 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
  • (def ppl (atom {"persons" {"joe" {:age 1}}}))
    (swap! ppl assoc-in ["persons" "bob"] {:age 11})
    
    @ppl
    {"persons" {"joe" {:age 1}, "bob" {:age 11}}}
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:4939 top

(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)))
Vars in clojure.core/assoc-in: assoc defn get
Used in 3 other vars extend header add-class

Comments top

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