I am new to Clojure and have found that when I loop over this vector in clojure using a list comprehension I get some nil
s at the end.
(def myVec [1,2,3])
user=> (for [x myVec] (println x))
(1
2
3
nil nil nil)
I get the same thing using map
user=> (map println myVec)
(1
2
3
nil nil nil)
What causes the nill to be printed in these cases?
for
and map
create a new lazy sequence with every element in the original vector replaced by the result of (println element
), and println
returns nil.
You should not be using for
and map
to perform side-effects (like printing) on the elements. Use doseq
for that.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With