On Mar 18, 8:08 pm, Shantanu Kumar <[email protected]> wrote:
> An alternate way of doing it:
>
> (defmacro maybe
> [& body]
> `(try [(do ~@body) nil]
> (catch Exception e#
> [nil e#])))
>
> (defn repeat-exec
> ([f]
> (let [run (fn g[] (cons (f) (lazy-seq (g))))]
> (run)))
> ([n f]
> (take n (repeat-exec f))))
>
> (defmacro try-times
> [n & body]
> `(let [f# #(first (maybe ~@body))
> c# (repeat-exec (dec ~n) f#)
> r# (some identity c#)]
> (or r# (do ~@body))))
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)]))))
Regards,
Shantanu
--
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