Some alternatives:
If you just want to add a bunch of elements to the collection contained in Ref, starting one transaction per element and conj them individually is a little wasteful. Instead, you could do
(defn init-items [] (dosync (alter items into ["foo" "bar" "baz"])))
If you have a reason to do this with one element per step, I think the most idiomatic and convenient approach for now would be to use doseq :
(defn init-items [] (doseq [item ["foo" "bar" "baz"]] (dosync (alter items conj item))))
(Or you can move the entire doseq to dosync and not use dosync in the body of doseq .)
MichaΕ Marczyk
source share