Note that doseq impatient and doseq is lazy. In an example missing from Rayne's answer,
(for [x [1 2 3]] (println x))
In REPL, this will usually do what you want, but this is basically a coincidence: REPL forces the lazy sequence created by for , causing printlns to appear. In a non-interactive environment, nothing will ever be printed. You can see it in action by comparing the results.
user> (def lazy (for [x [1 2 3]] (println 'lazy x))) #'user/lazy user> (def eager (doseq [x [1 2 3]] (println 'eager x))) eager 1 eager 2 eager 3 #'user/eager
Since the def form returns the newly created var, and not the value that is bound to it, nothing exists to print the REPL, and lazy will refer to the unrealized lazy-seq: none of its elements were calculated at all. eager will refer to nil , and all its printing will be executed.
amalloy Mar 13 '11 at 4:30 2011-03-13 04:30
source share