Fork me on GitHub
#code-reviews
<
2016-01-13
>
escherize15:01:09

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

ghadi15:01:20

seems fine to me escherize

ghadi15:01:38

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

ghadi15:01:56

(nor in the try)

escherize16:01:07

thanks! that's helpful

jonahbenton16:01:39

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

arcdrag17:01:46

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

meow19:01:09

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

ghadi19:01:39

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

meow20:01:06

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