code-reviews

escherize 2016-01-13T15:43:09.000343Z

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

ghadi 2016-01-13T15:59:20.000349Z

seems fine to me escherize

ghadi 2016-01-13T15:59:38.000350Z

you don't need the do block inside catch (most blocks in clojure are implicit do blocks)

ghadi 2016-01-13T15:59:56.000351Z

(nor in the try)

escherize 2016-01-13T16:00:07.000352Z

thanks! that's helpful

2016-01-13T16:06:39.000354Z

@escherize: also no need for atoms, can just use loop/recur with local state.

arcdrag 2016-01-13T17:58:46.000356Z

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

meow 2016-01-13T19:06:09.000360Z

where do you see the use of f (and I agree with you completely)

ghadi 2016-01-13T19:37:39.000361Z

@meow: the clash of the name flaky-fn as both arg and var

1👍
meow 2016-01-13T20:10:06.000363Z

@ghadi: gotcha! yes. don't do that