Clojure Assoc-in, Part 2
In a previous post, Clojure Assoc-in, I discussed adding items to a Clojure nested map. I came up with an alternative solution.
Let’s define the original map (again):
(def movie {:movie {:info {:name "Little Trouble in Big Manhattan"
:year "2020"
:format "BR-1000"}}})
=> #'ns-movies-db/movie
To recap, we want to add the following to the above map, under the :info
key:
{:actor "JeSuis VonBraun"
:language "French"}
Here’s the previous solution:
(assoc-in (assoc-in movie [:movie :info :actor] "JeSuis VonBraun")
[:movie :info :language] "French")
=>{:movie {:info {:name "Little Trouble in Big Manhattan",
:year "2020",
:format "BR-1000",
:actor "JeSuis VonBraun",
:language "French"}}}
And now, the updated one (drumroll please!):
(-> (assoc-in movie [:movie :info :actor] "JeSuis VonBraun")
(assoc-in [:movie :info :language] "French"))
=>
{:movie {:info {:name "Little Trouble in Big Manhattan",
:year "2020",
:format "BR-1000",
:actor "JeSuis VonBraun",
:language "French"}}}
Ok, I know, not terribly exciting, but it’s a little easier to understand (at least for me). And yes, we are using a thread-first macro like we discussed in a previous post on threading macros.
In Closing…
I am still not happy with this solution, and I’m sure there is still a more elegant way to solve this one. Stay tuned!