yayitswei
yayitswei

Reputation: 4677

unexpected output for map inside of do

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

Upvotes: 4

Views: 97

Answers (1)

Miki Tebeka
Miki Tebeka

Reputation: 13850

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")))

Upvotes: 7

Related Questions