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).
(assoc {} :key1 "value" :key2 "another value") ;;=> {:key2 "another value", :key1 "value"} ;; Here we see an overwrite by a second entry with the same key (assoc {:key1 "old value1" :key2 "value2"} :key1 "value1" :key3 "value3") ;;=> {:key3 "value3", :key2 "value2", :key1 "value1"} ;; We see a nil being treated as an empty map (assoc nil :key1 4) ;;=> {:key1 4} ;; 'assoc' can be used on a vector (but not a list), in this way: ;; (assoc vec index replacement) (assoc [1 2 3] 0 10) ;;=> [10 2 3] (assoc [1 2 3] 2 '(4 6)) ;;=> [1 2 (4 6)] ;; Next, 0 < index <= n, so the following will work (assoc [1 2 3] 3 10) ;;=> [1 2 3 10] ;; However, when index > n, an error is thrown (assoc [1 2 3] 4 10) ;; java.lang.IndexOutOfBoundsException (NO_SOURCE_FILE:0) ;; From http://clojure-examples.appspot.com/clojure.core/assoc
;; here is an example of updating a field in a map. (def test-map {:account-no 12345678 :lname "Jones" :fnam "Fred"}) (assoc test-map :fnam "Sue") ;;=> {:account-no 12345678, :lname "Jones", :fnam "Sue"} ;; notice that test-map is unchanged test-map ;;=> {:account-no 12345678 :lname "Jones" :fnam "Fred"})
;;transform a map´s values using reduce and assoc (defn transform [coll] (reduce (fn [ncoll [k v]] (assoc ncoll k (* 10 v))) {} coll)) (transform {:a 1 :b 2 :c 3}) ;;{:a 10 :b 20 :c 30}
;; convert a vector into a set with assoc (def book-city {:awards ["Hugo" "World Fantasy Award" "Arthur C. Clarke Award" "British Science Fiction Award"] :title "The City and the City" :authors [{:birth-year 1972, :name "China Miéville"}]}) (assoc book-city :authors (set (:authors book-city))) ;; {:awards ["Hugo" "World Fantasy Award" "Arthur C. Clarke Award" "British Science Fiction Award"], :title "The City and the City", :authors #{{:birth-year 1972, :name "China Miéville"}}}
;; You can also change multiple keys at once (def my-map {:name "Victor" :age 27 :city "Barcelona"}) (assoc my-map :name "Tom" :age 47) ;; {:name "Tom", :age 47, :city "Barcelona"}
;; You can add some more fields (kvs) to your map (assoc {:type "chicken"} :home "Kentucky" :cooking-method "fried" :tasty "definitely")
;; add new key-value from original key value (let [contact {:name "kimim" :home "Hangzhou"} new-name "ivy"] (assoc contact :modified? (not (= (contact :name) new-name)) :name new-name)) ;; {:name "ivy" :home "Hangzhou" :modified? true}
Associates a value in a nested associative structure, where ks is a sequence of keys and v is the ...
dissoc[iate]. Returns a new map of the same (hashed/sorted) type, that does not contain a mapping ...
Returns a map that consists of the rest of the maps conj-ed onto the first. If a key occurs in mo...
'Updates' a value in an associative structure, where k is a key and f is a function that will take...
Returns a new coll consisting of to-coll with all of the items of from-coll conjoined. A transduce...
Here is a version that will create a vector when the key is numerical. This may be useful instead of throwing an IndexOutOfBoundsException.
(defn assoc-in-idx [m [k & ks] v] (let [value (get m k (when (number? (first ks)) [])) m (if (and (vector? m) (number? k) (-> m count (< k))) (reduce (fn [m _] (conj m nil)) m (range (count m) k)) m) v (if ks (assoc-in-idx value ks v) v)] (assoc m k v)))