have to say I started learning Clojure about two weeks ago and now I'm stuck on a problem since three full days.
I got a map like this:
{
:agent1 {:name "Doe" :firstname "John" :state "a" :time "VZ" :team "X"}
:agent2 {:name "Don" :firstname "Silver" :state "a" :time "VZ" :team "X"}
:agent3 {:name "Kim" :firstname "Test" :state "B" :time "ZZ" :team "G"}
}
and need to change :team "X"
to :team "H"
.
I tried with a lot of stuff like assoc
, update-in
etc. but nothing works.
How can I do my stuff? Thank you so much!
assoc-in is used for replacing or inserting values in the map specified by path
however, if you want to update ALL team "X"'s, regardless of specific path, over all recursive levels of the tree, you can use clojure.walk's prewalk or postwalk functions combined with a function of your own:
The walking functions are good for replacement like that.
(clojure.walk/prewalk-replace {[:team "X"] [:team "H"]} map)
Passing in vectors allows you to ensure that you don't just replace all the "X"s.