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
  • You can use update-in. Commented Jul 26, 2014 at 16:49

1 Answer 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. Commented Apr 5, 2015 at 15:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.