2010/11/29 Jozef Wagner <jozef.wag...@gmail.com>

> I think it is because concurrent stuff can happen between let and
> alter in the first example. Is it true that in that case, one would
> have to use "ensure" to make the operation correct?
>

That's my point (at least the way I understand it, I do not use a lot of
concurrency features of clojure yet) :

you would have to use ensure if you were to rely on the read value for the
computation of another value, but since you alter the ref, there should be
no need to ensure, and the problem would not be with "correctness", more
with "performance" (avoid transaction retries).

Hmm, that's maybe what Stu wanted to say by "concurrency *friendly*", after
all ....


>
> On Nov 29, 5:53 pm, Laurent PETIT <laurent.pe...@gmail.com> wrote:
> > 2010/11/29 Stuart Halloway <stuart.hallo...@gmail.com>
> >
> > > 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.
> >
> > Yep, and in this case it *seems* that using commute could be useful as
> well
> > (and thus one more point for the latter form).
> >
> > Though, I fail to see from a point of view of "correctness", why the
> former
> > form is not concurrency-friendly ?
> >
> >
> >
> >
> >
> >
> >
> >
> >
> > > 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<clojure%2bunsubscr...@googlegroups.com>
> <clojure%2bunsubscr...@googlegroups.com<clojure%252bunsubscr...@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<clojure%2bunsubscr...@googlegroups.com>
> <clojure%2bunsubscr...@googlegroups.com<clojure%252bunsubscr...@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<clojure%2bunsubscr...@googlegroups.com>
> <clojure%2bunsubscr...@googlegroups.com<clojure%252bunsubscr...@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<clojure%2bunsubscr...@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

Reply via email to