Hello

I've recently found out just what a good invention agents are. I find
them to handle a few idioms I see in other languages very nicely. I
started changing some of my code over to it, and it has really helped
the structure. But I was a little surprised to find out that by
default, if you don't return anything, it will set the value of the
agent to nil. I see two problems with this: it's makes the code
verbose and it leads to subtle bugs. So I cooked up a little macro (be
gentle, it's my first real macro :)) to on nil, just return the state
instead.

(defmacro defa [name args & form]
  `(defn ~name [...@args]
     (let [result# (do ~...@form)]
       (cond (not result#)
             (first ~args)
             :else
             result#))))

Hopefully somebody will find it useful, I was contemplating if it
should also add state as the first parameter to the function, sort of
like this in OO languages, but I decided not to. It's a bit intrusive
and also my macro knowledge didn't allow me to write it ;-)

-- 
Anders Rune Jensen
http://people.iola.dk/arj/

-- 
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