tags:

views:

64

answers:

1

Why doesn't this produce the output I expect?

(defn test-fn []
  (do
    (println "start")
    (map #(println (+ % 1)) '(1 2 3))
    (println "done")))

It outputs

start
done

Whereas I would expect

start
2 3 4
done
+6  A: 

map is lazy, and do does not force it. If you want to force the evaluation of a lazy sequence, use doall or dorun.

(defn test-fn []
  (do
    (println "start")
    (dorun (map #(println (+ % 1)) '(1 2 3)))
    (println "done")))
lazy1
perfect.. thanks!
yayitswei
don't do (+ % 1) its harder to read and slower then (inc %)
nickik