Skip to content

Instantly share code, notes, and snippets.

@mtm
Created May 11, 2012 18:16
Show Gist options
  • Save mtm/2661471 to your computer and use it in GitHub Desktop.
Save mtm/2661471 to your computer and use it in GitHub Desktop.
alternate periodic function
(def ^{:dynamic true} *print-progress* true)
(defmacro defn-do-periodic [name args [var period] body]
`(let [counter# (atom 0)]
(defn ~name [~@args]
(swap! counter# inc)
(when (and *print-progress*
(zero? (mod @counter# ~period)))
(let [~var @counter#]
~body)))))
;; Let's define a periodic logger
(defn-do-periodic log-progress [merchant] [n 100]
(println (str "we've processed" n "widgets for" merchant)))
;; Usage
(doseq [widget (get-widgets)]
(process-widget widget)
(log-progress some-merchant))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment