On Fri, Mar 18, 2011 at 1:13 PM, Shantanu Kumar
<kumar.shant...@gmail.com> wrote:
> The `try-times` macro above is buggy (doesn't work when body of code
> returns logical false). Fixed version is below:
>
> (defmacro try-times
>  [n & body] {:pre [(posnum? n)]}
>  `(let [c# (repeat-exec (dec ~n) #(maybe ~@body))
>         r# (some #(if (last %) nil %) c#)]
>     (first (or r# [(do ~@body)]))))

You might want to unwrap everything after:

(defmacro try-times
  [n & body] {:pre [(posnum? n)]}
  `(let [c# (repeat-exec (dec ~n) #(maybe ~@body))
         r# (some #(if (last %) nil %) c#)
         [r# e#] (first (or r# [(do ~@body)]))]
     (if e#
       (throw e#)
       r#)))

This will re-throw the last exception on failure. On success it will
evaluate to the return value of the successful execution of the body.
So this try-times trying to acquire a network socket may throw a
socket unavailable exception or return the socket, rather than
returning a vector of a maybe-socket and a maybe-exception.

The other thing you will probably want is a delay:

(defmacro try-times
  [delay n & body] {:pre [(posnum? n)]}
  `(let [c# (repeat-exec (dec ~n) #(do (maybe ~@body) Thread/sleep ~delay))
         r# (some #(if (last %) nil %) c#)
         [r# e#] (first (or r# [(do ~@body)]))]
     (if e#
       (throw e#)
       r#)))

Now you can (try-times 30000 10 (acquire-socket 80)) to spend five
minutes trying to grab a socket at half-minute intervals, throw on
failure, and return the socket on success. That would be quite handy
for low level networking, where you usually want to wait a bit between
retries for congestion to ease or other conditions to change that were
impeding success.

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