ClojureDocs

导航

命名空间

关联

clojure.core

自 1.0 开始可用 (源代码)
  • (assoc map key val)
  • (assoc map key val & kvs)
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).
9 Examples
(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"})
;; beware of this
(assoc {} nil nil)
;;=> {nil nil}
;;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}
;;assoc applied to a vector

(def my-vec [1 2 5 6 8 9])

(assoc my-vec 0 77)
;;[77 2 5 6 8 9]
;; 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}
See Also

Associates a value in a nested associative structure, where ks is a sequence of keys and v is the ...

Added by alimoeeny

dissoc[iate]. Returns a new map of the same (hashed/sorted) type, that does not contain a mapping ...

Added by pauldoo

Returns a map that consists of the rest of the maps conj-ed onto the first. If a key occurs in mo...

Added by boxie

'Updates' a value in an associative structure, where k is a key and f is a function that will take...

Added by MicahElliott

Returns true if coll implements Associative

Added by xmo-odoo

Returns a new coll consisting of to-coll with all of the items of from-coll conjoined. A transduce...

Added by MicahElliott
2 Notes
    By , created 12.5 years ago

    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)))
    
    By , created 11.6 years ago

    the API is blurry When applied to a vector

    ;; should indicate following
    (assoc vector index val)