I must respectfully disagree with James's first point here. The first pattern (read-ponder-update) is not concurrency-friendly. It isn't about atom vs. ref, the important distinction is whether all the work can be done in a function that gets sent *to* the ref. The latter formulation also can be extracted into a pure function, which is easier to test.
The bang suffix "!" is generally reserved for functions that are not safe in a transaction. If you are using atoms, you should have the bang, if using refs, you shouldn't. Stu > I'd say the former was more idiomatic Clojure. There's no need to make > refs compatible with atoms (otherwise why have two different > concurrency primitives in the first place). > > Additionally, earmuffs (like *this*) should only be used on vars that > you expect to override in a binding, like *out* or *err*. They aren't > for all global vars. You should rename "*counts*" to just "counts". > > - James > > On 28 November 2010 22:36, Takahiro Hozumi <fat...@googlemail.com> wrote: >> Hi, >> This is trivial question. >> Which style do you like? >> >> (def *counts* (ref {})) >> >> (defn dec-or-dissoc! [key] >> (dosync >> (let [n (@*counts* key)] >> (if (< 1 n) >> (alter *counts* assoc key (dec n)) >> (alter *counts* dissoc key))))) >> >> (defn dec-or-dissoc! [key] >> (dosync >> (alter *counts* >> (fn [m] >> (let [n (m key)] >> (if (< 1 n) >> (assoc m key (dec n)) >> (dissoc m key))))))) >> >> I think former style is normal, but latter is easy to replace ref with >> atom. >> Thanks. >> >> -- >> Takahiro Hozumi >> >> -- >> You received this message because you are subscribed to the Google >> Groups "Clojure" group. >> To post to this group, send email to clojure@googlegroups.com >> Note that posts from new members are moderated - please be patient with your >> first post. >> To unsubscribe from this group, send email to >> clojure+unsubscr...@googlegroups.com >> For more options, visit this group at >> http://groups.google.com/group/clojure?hl=en > > -- > You received this message because you are subscribed to the Google > Groups "Clojure" group. > To post to this group, send email to clojure@googlegroups.com > Note that posts from new members are moderated - please be patient with your > first post. > To unsubscribe from this group, send email to > clojure+unsubscr...@googlegroups.com > For more options, visit this group at > http://groups.google.com/group/clojure?hl=en -- You received this message because you are subscribed to the Google Groups "Clojure" group. To post to this group, send email to clojure@googlegroups.com Note that posts from new members are moderated - please be patient with your first post. To unsubscribe from this group, send email to clojure+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/clojure?hl=en