Use fnil to handle the nil value of v when k doesn't exist in the map:
(update m k (fnil conj []) v)
3
votes
While the fnil answer is more spectacular, I find the following easier to read, especially if unfamiliar with fnil:
(assoc m k (conj (m k []) v))
where (m k []) returns the value of k in m or defaults to [] if k does not exist in m.
If k is a symbol, (k m []) would also work.
We use cookies to ensure that we give you the best experience on our website. If you continue to use this site we will assume that you are happy with it.OkRead more