Supposing I has a data structure like this:
[[{:name "bob" :favorite-color "green"}{:name "tim" :favorite-color "blue"}]
[{:name "eric" :favorite-color "orange"}{:name "jim" :favorite-color "purple"}]
[{:name "andy" :favorite-color "green"}{:name "tom" :favorite-color "blue"}]]
and an array like this:
["green" "purple"]
How would I pass over my data structure and augment all maps for folks who liked the colors in my array with a new key value pair of :likes-my-colors "yes" ?
The result would be:
[[{:name "bob" :favorite-color "green" :likes-my-colors "yes"}{:name "tim" :favorite-color "blue"}]
[{:name "eric" :favorite-color "orange"}{:name "jim" :favorite-color "purple" :likes-my-colors "yes"}]
[{:name "andy" :favorite-color "green" :likes-my-colors "yes"}{:name "tom" :favorite-color "blue"}]]
(I intentionally made the value a string of yes as opposed to true because that's closer to what I am trying to figure out).
I tried loop and recur with postwalk but couldn't figure out how to mutate the map with subsequent recursions. I won't paste my horrid attempt here because I am guessing there's a better way to do it then with recur. However, postwalk would have the advantage of being able to handle more an increasingly nested data structure, which will likely be the case. So maybe recur with postwalk is the way to go.
I'm using ClojureScript and Reagent to store app state in an atom... as things occur I need to keep updating the app state in that atom. The app state gets reset repeatedly in a single user session... it gets built up and modified after each reset. As in this example, the app state gets modified based on arrays. My code needs to work through the elements of the array and modify all the maps that meet a condition. Eventually, this structure is used to add classes to a Hiccup data structure. The UI changes accordingly; people in a list would have borders appear around them if they liked my colors, for example, by having a class added.
I had awesome help in learning how to look through a data structure like this and update all maps given a specific key/value pair... but I've run into trouble doing it with a series of values. In other words, 'build up' a map in a sense... but it's more 'modify with multiple passes'. That phrasing will hopefully improve as my understanding does.
I am wondering, as a side note, how Clojure users go about accessing and mutating elements buried deeply in nested data structures. I'd rather have more complicated data structures but I avoid them because it seems hard to modify deep elements. I'm suspecting they might use libraries. It seems like there may be an easier way of getting at and modifying complex structures than writing brain teaser (for me) code. But then again, I may be wrong. There are a lot of examples online but they are often about modifying simple structures.
mapor specter would do.