I need to write some stuff to a file based on elements of a Clojure collection, which I can do--but I ran into something that confuses me. It's probably because I don't fully understand the time macro, but when I do the following:
=> (def nums (take 100000 (repeatedly #(str (rand-int 1000) " "))))
(defn out1 [nums] (doseq [n nums] (spit "blah1.txt" n :append true)))
(defn out2 [nums] (map #(spit "blah2.txt" % :append true) nums))
#'test.core/nums
#'test.core/out1
#'test.core/out2
=> (time (out1 nums))
"Elapsed time: 19133.247 msecs"
nil
=> (time (out2 nums))
"Elapsed time: 0.209 msecs"
(nil nil nil nil ... )
the implementation using map (out2) runs significantly faster. However, when I go to the folder and watch the file, it continues to do the writing after the Elapsed time is given and the (nil ...) output waits until it's done writing to show also. That leads me to believe that they're both actually taking the same time.
So, what's the difference between using doseq and map in this situation? And which way would be better overall? Thanks