Hi,

Am 19.07.2009 um 06:06 schrieb Rowdy Rednose:

(defn db-push
 ([key val] (db-push key val *default-db*))
 ([key val db]
    (swap! db assoc key (cons val (@db key)))))

But I think it's broken in the face of concurrency, as I capture the
value of @db at the time of the call to the function swap!

I think the correct way is to make the whole assoc an fn.

(swap! db #(assoc % key (cons val (% key))))

As for Var vs. Atom vs. Ref:

I use Var only to hold functions, macros or constants.
When you never have to coordinate your db with
something else you can use an Atom. If you need to
coordinate changes eg. in two different dbs, you'll
need a Ref.

  (dosync
    (add-entry db1 entry)
    (remove-entry db2 entry))

For the default arguments:

Yes. Different arities are usually used for this.
As Richard said you can also emulate keyword
arguments. For this there is also defnk in
clojure.contrib.def to make this a bit more
comfortable.

Sincerely
Meikel

Attachment: smime.p7s
Description: S/MIME cryptographic signature

Reply via email to