On Dec 3, 2008, at 7:19 PM, aria42 wrote:

> Hi all,
>  When I run the following from the REPL, I see the result of the
> "println"
>
> (defn read-docs [duc-dir]
>  (for [file (.listFiles (java.io.File. duc-dir))
>          :when (.isFile file)]
>       (do
>         (println file)
>         (.getName  file))))
>
>
>  However, when embedded in a script (using clojure.lang.Script), I
> don't see the result of the println. I'm primarily using it for
> debugging the compreshension variables.
>
>   Thanks a lot, Aria

Hi Aria,

The result returned by the "for" function is a lazy sequence. It will  
only produce as many values as are actually requested by its caller.  
In the case of the repl, the "print" part of the read-eval-print-loop  
will force it to produce all the results. In the case of Script, there  
is nothing to force the output, so it will never be produced and  
println won't be called.

If your primary goal is the side-effect produced by "println", doseq  
(which has recently been upgraded to be a complete non-lazy  
counterpart to "for") should do the trick. Just drop it in in place of  
"for". (it will ignore the result returned by (.getName file) in that  
case, so that can and should be removed.) Another way to do this would  
be to pass the result of "for" to "dorun".

If your primary goal is to get the sequence of names, continue to use  
"for" and avoid the "println", but understand that unless you do  
something with the result, it won't actually be produced. A way to  
"force" all items to be evaluated is to pass the result of "for" to  
"doall".

In either case, you'll avoid the confusing mixture of output that gets  
printed by the code you posted.

--Steve


--~--~---------~--~----~------------~-------~--~----~
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
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/clojure?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to