Skip to content

Instantly share code, notes, and snippets.

@mtm
Forked from kriyative/progress.clj
Created May 11, 2012 18:08
Show Gist options
  • Save mtm/2661412 to your computer and use it in GitHub Desktop.
Save mtm/2661412 to your computer and use it in GitHub Desktop.
First cut implementation of `with-progress` and `report!`
(def ^{:dynamic true} *print-progress* true)
(defmacro with-progress
"Bind a `reportfn` function, and evaluate `body` wherein
calling (report!) will invoke the report function with the current
state of the iteration."
[reportfn & body]
`(let [iter# (atom 0)
reporter# ~reportfn]
(letfn [(~'report! []
(do
(swap! iter# inc)
(when *print-progress*
(reporter# (deref iter#)))))]
~@body)))
(defn fprint [& more]
"Same as print but explicitly flushes *out*."
(apply print more)
(flush))
(defn fprintln
"Same as println but explicitly flushes *out*."
[& more]
(apply println more)
(flush))
(defn make-standard-reporter
"Generate a standard reporter function which can be passed to
`with-progress`."
[n & {:keys [ncols line-handler]}]
(fn [i]
(cond
(zero? (mod i n))
(fprintln (format "%,8d rows%s"
i
(if line-handler (str " " (line-handler i)) "")))
(zero? (mod i (int (/ n (or ncols 70))))) (fprint "."))))
@mtm
Copy link
Author

mtm commented May 11, 2012

made a small change to continue to increment the counter when print-progress is false

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment