> (defn dec-or-dissoc! [key]
> (dosync
> (let [n (@*counts* key)]
> (if (< 1 n)
> (alter *counts* assoc key (dec n))
> (alter *counts* dissoc key)))))
> Is it true that in that case, one would have to use "ensure" to make the
> operation correct?
I don't think ensure is necessary here, coz the *counts* gets altered
in all cases.
Whereas consider this:
(defn dec-or-dissoc! [key]
(dosync
(let [n (@*counts* key)]
(when (< 1 n)
(alter *counts* assoc key (dec n))))))
In this case I think ensure is necessary.
Now consider this,
(defn dec-or-dissoc! [key]
(dosync
(alter *counts*
(fn [m]
(let [n (m key)]
(when (< 1 n)
(assoc m key (dec n))))))))
In this case ensure is not necessary.
--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to [email protected]
Note that posts from new members are moderated - please be patient with your
first post.
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en