Hi, 2010/6/17 William Wadsworth <will.wadsworth...@gmail.com>: > Hi. > > I have just started learning Clojure and I am really enjoying > myself. However, I am still getting to grips with the workings of its > concurrency model, so please excuse me if my question seems too > obvious. > > I have some code that reads data from a file, parses it and generates > a CSV file. The flow is as follows: > > <code> > (use 'clojure.contrib.duck-streams) > (use 'clojure.contrib.pprint) > > (defn parse-line [s] > (cl-format nil "~{\"~A\"~^,~}" (into [] (.split s ";")))) > > (let [input-seq (for [line (read-lines "input.dat")] > (parse-line line))] > (write-lines "out.CSV" input-seq)) > </code> > > Now the question: > > I would like to generate log entries for any malformed lines occurring > in the input file. Once of the ways might be to do this in parse-line > using clojure.contrib.logging as follows: > > <code> > (use 'clojure.contrib.logging) > > (defn parse-line [s] > (let [cs (into [] (.split s ";"))] > (if (= (count cs) 5) > (cl-format nil "~{\"~A\"~^,~}" cs) > (error "Incorrect number of columns in file.")))) > </code> > > Since FOR returns a lazy sequence of the results, is this function > safe? (Seeing that it is not side-effect free?)
You have to acknowledge that the calls to parse-line will not necessarily occur during the call to for, but maybe not before calling write-lines, maybe partly if chunked-seqs are in play (sequences preloading items in chunks of 32 items for example). And, if there is an exception that is not thrown in write-lines, your logs may miss the error logs for some of the remaining items not yet consumed by write-lines. > BTW: > clojure.contrib.logging indicates that it can use an agent for > logging. Is my assumption that invoking the logger (with or without > an agent) within a function would still mean that the function has > side-effects correct? Technically speaking, yes. Imagine there's an I/O exception thrown from the logging call (disk full, no write access, network access, etc.), then your function would not be "safe" anymore. -- 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