Q:

Updating multiple values in a single command in clojure

If I have
(def mymap {"a" {:x 0 :y 0 :z 0}})

And I want to increase each of x, y, z in "a" by dX, dY, dZ respectively, can I use update-in in one command or do I need to do it separately?

A:

(let [mymap {"a" {:x 10 :y 10 :z 10}}]
    (update mymap "a" (fn [x] (merge-with + x {:x 1 :y 2 :z 3}))))

A:

Probably you want something like update-coords:
(def mymap {"a" {:x 0 :y 0 :z 0}})

(defn update-coords [m dx dy dz]
  (update-in m ["a"] (partial merge-with +) {:x dx :y dy :z dz}))

;; (update-coords mymap 1 2 3)
;; => {"a" {:x 1, :y 2, :z 3}}

update-coords takes a map m and the three deltas dx, dy and dz, and "updates" the entry on the key "a" of the map m with a function that will perform merge-with between the map under the key "a" and a new map, with the deltas put in the right places so that the keys overlap. merge-with will find the repeated keys :x, :y and :z and will use + to produce a new value that will be used in the output. If mymap contains extra keys that don't overlap, they are just copied to the output.

