I want to repeatedly executes a function for its side-effects on a collection.
The common way is:
(defn my-func [x] (println x))
(doseq [x '(1 2 3) ]
(my-func x))
But the x variable is a little bit cumbersome. I prefer the following code:
((comp doall map) my-func '(1 2 3))
Do you think this is an elegant solution?
clojure.core/dorunis better thandoall, becausedoallholds the whole sequence in memory in order to return it to you.dorunreturnsnil, just asdoseqdoes. To say that the language core “should” offer something likedomapis a bit aggressive – for a language that discourages mutation and side effects, Clojure already has plenty of functions that are very close to what you want and are easy to combine.