On Tue, Aug 16, 2011 at 11:26 AM, Thomas <th.vanderv...@gmail.com> wrote: > Hi everyone, > > I have been struggling with this, hopefully, simple problem now for > quite sometime, What I want to do is: > > *) read a file line by line > *) modify each line > *) write it back to a different file > > This is a bit of sample code that reproduces the problem: > > ========================================================== > (def old-data (line-seq (reader "input.txt"))) > > (defn change-line > [i] > (str i " added stuff")) > > (spit "output.txt" (map change-line old-data)) > ========================================================== > #cat "output.txt" > clojure.lang.LazySeq@58d844f8 > > Because I get the lazy sequence I think I have to force the execution? > but where > exactly? And how?
The spit function expects a string; you need to pr-str the object. However, that will output it like the REPL would: (line1 line2 line3 ...) You probably want no parentheses, and separate lines. So you'll want something more like (with-open [w (writer-on "output.txt")] (binding [*out* w] (doseq [l (map change-line old-data)] (println l)))) The output part is lazy now, so you might want to consider making the input part lazy as well: (with-open [r (reader-on the-input-file) w (writer-on "output.txt")] (binding [*out* w] (doseq [l (line-seq r)] (println (change-line l))))) (note: untested, and assumes suitable reader-on and writer-on functions such as from contrib) Then it will be able to process files bigger than can be held in main memory all at once. -- Protege: What is this seething mass of parentheses?! Master: Your father's Lisp REPL. This is the language of a true hacker. Not as clumsy or random as C++; a language for a more civilized age. -- 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