Clojure and me has moved.

Thursday, January 8, 2009

try-or, or-try, try-else or else-try?

This post has moved, go to its new location
I can't decide which name is best for this macro:
(defmacro try-or
"Evaluates exprs one at a time, from left to right. If a form returns a
value, this value is returned. If a form throws an exception, the next
form is evaluated.
If the last form throws an exception, the exception isn't caught."
([] nil)
([form] form)
([form & forms]
`(try
~form
(catch Exception e#
(try-or ~@forms)))))

7 comments:

Shawn said...

try-cond

Shawn said...

No, never mind. try-or

lpetit said...

why not 'try-one ?

grayswx said...
This comment has been removed by the author.
grayswx said...

It most closely matches the existing function some. So maybe try-some?

Christophe Grand said...

grayswx: some is a function (plus it takes a predicate), or is a macro. I think try-or is better.

TheArthur said...

try-harder!