Search code examples
collectionsclojure

How to add an element in nested atom map - Clojure


I getting that error: Cannot invoke "clojure.lang.IAtom.swap(clojure.lang.IFn, Object, Object)" because "atom" is null.

I want to add a keyword value in an atom map. Where am I doing mistakes? (I tried reduce too)




(def !my-map (atom {1 {:id   1
                       :name {:first "ali" :last "veli"}}
                    2 {:id   2
                       :name {:first "batu" :last "can"}}}))
;expected output:
;        {1 {:id 1
;  :name {:first "ali" :last "veli" :fullname "ali veli"}}
;         2 {:id 2
;  :name {:first "batu" :last "can" :fullname "batu can"}}}


((fn [y]
   (let [{:keys [first last]} y]
     (swap! (get-in @!my-map [:name]) assoc :fullname (str first " " last))
     ))
 (for [len (range 1 (+ 1 (count @!my-map)))]
   (get-in @!my-map [len :name])
   )
;=> ({:first "ali", :last "veli"} {:first "batu", :last "can"})
)




Solution

  • (swap! !my-map update-vals
           (fn [v]
             (update v :name
                     (fn [n]
                       (let [{:keys [first last]} n]
                         (assoc n :fullname (str first " " last)))))))