clojure - How do i get a modified Set after updating element for a specific key? -
i have set of objects
(def books #{{:isbn 1 :title "programming clojure"} {:isbn 2 :title "joy of clojure"} {:isbn 3 :title "clojure in action"}})
how update object having given key (:isbn) , return modified set?
(??? books :isbn {:isbn 1 :title "programming clojure" :author "halloway"})
in java, equality can defined using isbn , element can directly added set. idiomatic way of doing in clojure?
if want kind of updates, should have associative structure. can turn set 1 , again:
(-> (group-by :isbn books) (assoc-in [1 0 :author] "halloway") ; 1 isbn, 0 means "first" vals (->> (map first)) set)
this code assumes :isbn unique (since said "key"). turns set map :isbn values sequences of corresponding records, updates first record :isbn 1
author, mangles set.
Comments
Post a Comment