Search code examples
clojureleiningen

Clojure map runs in cider, but not lein run


I'm having an issue where my project runs in Cider, but not with lein run.

Consider these functions in core.clj:

(def my-vec ["a" "b" "c"])

(defn dostuff [x] (spit "/home/dirty/file.txt" x :append true))

(defn -main [& args] (map dostuff my-vec))

Now, when I open Cider repl and enter (-main), this will run and file.txt will contain "abc...". However, if I go to the project with the terminal and run lein run it runs for a few moments and then shuts down. But file.txt is not written to. What am I overlooking?


Solution

  • map is a lazy sequence, it's not meant for side effects. If you want to force the realization of the lazy sequence, you need to wrap your map in a doall. You shouldn't be using map for this in the first place as you don't care about the resulting sequence. See doseq:

    (doseq [x my-vec] (dostuff x))