How can I update a vector item in Clojure?

clojure

Solution

map a function over the vector of maps that either creates a modified map if the key matches or uses the original if the keys don't match then turn the result back into a vector

(vec (map #(if (= (:id %) 1) 
             (assoc % :a "baz2" :b "spam2")
             %)))

It is possible to do this more succinctly though this one really shows where the structural sharing occurs.

Problem

Given: ``` (def my-vec [{:id 0 :a "foo" :b "bar"} {:id 1 :a "baz" :b "spam"} {:id 2 :a "qux" :b "fred"}]) ``` How can I idiomatically update * the item in my-vec with `:id=1` to have values `:a="baz2"` and `:b="spam2"`? *: I recognize that I wouldn't actually be updating my-vec, but really returning a new vector that is identical to my-vec except for the replacement values.

Original source