> Clojure REPL will display nil for anything that causes side-effect?
> I had no idea!

More accurately, any side effects that print will be printed, then the  
return value of the function will be printed by the REPL (the "P" part).

If the function returns nil, you'll see a final nil appear.

map collects the return values of its calls and returns the sequence.  
println returns nil, so

(map (fn [x] (println "Foo" x)) [1 2 3])

will print

Foo 1
Foo 2
Foo 3

and return (nil nil nil). This list will be printed by the REPL.

Strictly speaking, because map is lazy it will actually show in the  
REPL:

user=> (map (fn [x] (println "Foo" x)) [1 2 3])
(Foo 1
Foo 2
Foo 3
nil nil nil)


You'll see that the side effects of printing involved in calculating  
the expression occur part-way through printing the list of nils in the  
REPL! Laziness in action :)

If you force the matter with doall this changes:


user=> (doall (map (fn [x] (println "Foo" x)) [1 2 3]))
Foo 1
Foo 2
Foo 3
(nil nil nil)


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to