Skip to content

Instantly share code, notes, and snippets.

@roman
Created November 28, 2011 02:32
Show Gist options
  • Save roman/1398844 to your computer and use it in GitHub Desktop.
Save roman/1398844 to your computer and use it in GitHub Desktop.
clojure's repeatedly gotcha
(ns example)
; This won't work.
(defn wrong-process-lines [n action]
(letfn [(process []
(let [ln (read-line)]
(action ln)))]
(repeatedly n process)))
; The dorun call is going to evaluate each item of the lazy-seq returned from the
; repeatedly call. Ergo everything will work as expected.
(defn process-lines [n action]
(letfn [(process []
(let [ln (read-line)]
(action ln)))]
(dorun (repeatedly n process))))
(defn main [& args]
(process-lines 5 println))
(main)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment