When the docs say not to use side-effects in a function or block of code,
they're not saying you can't use side-effects at all; it's just a reminder
that using side-effects can cause nasty surprises for one or more of these
reasons:
1. The code may be evaluated later than you expect.
2. It might not be evaluated at all.
3. It might be evaluated more than once (with dosync, for instance).

Even though logging is technically all about side-effects, logging is not
normally considered a side-effect because most programs don't rely on
logging for correctness. Also, it's totally possible to mix side-effects
with lazy streams in a sane way; the dorun and doall functions exist for
just that purpose. Just use your judgement and don't rely on side-effects
more than you have to.

On Thu, Jun 17, 2010 at 11:07 AM, William Wadsworth <
will.wadsworth...@gmail.com> wrote:

> 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?)
>
> 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?
>
> Martin.
>
> --
> 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<clojure%2bunsubscr...@googlegroups.com>
> For more options, visit this group at
> http://groups.google.com/group/clojure?hl=en
>

-- 
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

Reply via email to