clojure - How do I update a record in a vector, matching certain criteria? -
i'm trying update records in vector, match criteria.
(defrecord item [id name description]) (def items (ref [ (->item "1" "cookies" "good tasting!") (->item "2" "blueberries" "healthy!") ]) )
how can e.g. "set name of item "foo" id equal 1"?
i maybe need like
(dosync (commute items ???? ))
can't figure ????
i found e.g. function update-in in docs
but 1. can't find examples records, 2. not sure if can use update different field 1 i'm using query. in examples fields seem same.
the complete use case: have webservice update operation, map id of item , optional fields have updated.
i'm new clojure. implemented remove function, id, works:
(commute items #(remove (fn [x](= (:id x) id)) %))
also find id, might step in direction update:
(nth (filtered (filter #(= (:id %) id) @items)) 0)
but don't know how update record in vector...
you can use assoc
make copy of record keys replaced.
(dosync (commute items #(mapv (fn [i] (if (= (:id i) "1") (assoc :name "foo") i)) %)))
Comments
Post a Comment