Created
November 28, 2011 02:32
-
-
Save roman/1398844 to your computer and use it in GitHub Desktop.
clojure's repeatedly gotcha
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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