0

I'm pretty sure there has to be a more idiomatic way of writing this:

(defn update-2nd-level-vector
  [map index value]
  (assoc map :key1 (assoc (get map :key1) :key2 (-> map (get-in [:key1 :key2]) (assoc index value)))))

Example of its working:

=> (update-2nd-level-vector {:key1 {:key2 [0 1]}} 0 1)
{:key1 {:key2 [1 1]}}
m0skit0
  • 175

1 Answers1

0

you must use update-in..also your function is not enough flexible, you're restricted to use it only in maps with those keys..

this is a bit more practical

(defn update-level [keyspos mapp index value]
  (assoc-in mapp keyspos (assoc (get-in mapp keyspos) index value)))

you can use it

(update-level [:key1 :key2] {:key1 {:key2 [0 1]}}  0 3)

or similar to your function

(def update-2nd-level-vector2  (partial update-level [:key1 :key2]))

(update-2nd-level-vector2 {:key1 {:key2 [0 1]}} 0 3)
clagccs
  • 116