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]}}
1
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)
1
  • Thanks. The function is just an example, I'm not using that exact function. – m0skit0 Apr 5 '15 at 15:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.