rfcr:
(defn with-retries
"Here flaky-fn is a function that tends to throw
many exceptions, yet works randomly when it doesn't.,"
[n flaky-fn]
(let [retries (atom n)
return (atom ::with-retries-failed)]
(while (< 0 @retries)
(try
(do
(reset! return (flaky-fn))
(reset! retries 0))
(catch Exception e
(do (println "failed on retry# " @retries)
(swap! retries dec) ))))
@return))
(defn flaky-fn []
(when (< 0.1 (rand)) (throw (Exception. "wat a flake.")))
"Meow! =^.^=")
(frequencies (repeatedly 1000 #(with-retries 20 flaky-fn)))
seems fine to me escherize
you don't need the do
block inside catch (most blocks in clojure are implicit do
blocks)
(nor in the try)
thanks! that's helpful
@escherize: also no need for atoms, can just use loop/recur with local state.
I would also be cautious in naming a parameter the same thing that you name a function in your namespace. It makes debugging a bit confusing. f
is typically the name used for "some completely generic function".
where do you see the use of f
(and I agree with you completely)
@meow: the clash of the name flaky-fn
as both arg and var
@ghadi: gotcha! yes. don't do that