See the documentation for assoc
:
=> (doc assoc)
-------------------------
clojure.core/assoc
([map key val] [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).
nil
assoc
doesn't take a map. It takes pairs of keys and vals:
user=> (assoc {} :a 1 :b 2)
{:a 1, :b 2}
user=> (let [x (atom {})]
#_=> (swap! x assoc :a 1 :b 2)
#_=> x)
#object[clojure.lang.Atom 0x227513c5 {:status :ready, :val {:a 1, :b 2}}]
By the way, you should always isolate your updates to an atom to a single swap!
. By doing two swaps as above, you're allowing other threads to potentially clobber the referenced data. A single swap!
keeps everything atomic.
N.B. merge
behaves as you were imagining:
user=> (merge {} {:a 1 :b 1})
{:a 1, :b 1}
user=> (let [x (atom {})]
#_=> (swap! x merge {:a 1 :b 2})
#_=> x)
#object[clojure.lang.Atom 0x1be09e1b {:status :ready, :val {:a 1, :b 2}}]