I don't think that will work. for is lazy, so by the time it evals, the
code will have dropped out of the scope of with-open.

So:

(defn read-stuff-2 []
  (with-open [r (java.io.BufferedReader.
                 (java.io.FileReader. "myfile.txt"))]
    (for [line (line-seq r)]
      line)))


fails with an IOException.

So, I think the OP needs:

(defn read-stuff-3 []
  (with-open [r (java.io.BufferedReader.
                 (java.io.FileReader. "myfile.txt"))]
    (doall
     (for [line (line-seq r)]
       line))))

or alternatively:

(defn read-stuff-4 []
  (with-open [r (java.io.BufferedReader.
                 (java.io.FileReader. "myfile.txt"))]
    (for [line (doall (line-seq r))]
      line)))

I really wish there were support for this in clojure.core: both "dofor"
and "domap" would be very useful. Laziness is useful but when working
with Java objecs with state, or anything with dynamic scope liberal use
of doall is necessary which just adds extra brackets. 

Phil



Jim <jimpil1...@gmail.com> writes:

> Only use 'doseq' when you don't care about the reuturn value. In other words
> only for side-effect-y code. Use 'for'  instead...
>
> Jim
>
>
>
> On 21/06/13 11:17, Jay C wrote:
>> Hi, I'm fairly new to Clojure and need help with a problem. The following
>> function always returns nil, whereas it should return the value of "line"
>> (which is not nil - I've tested).
>>
>>     (defn find-line-in-output [regex]
>>             (with-open [rdr (reader belarc-output-filepath)]
>>                     (doseq [line (line-seq rdr)]
>>                             (if (not (nil? (re-find (re-pattern regex)
>>     line)))
>>                                     line
>>                             )
>>                     )
>>             )
>>     )
>>
>> -- 
>> -- 
>> 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
>> ---
>> You received this message because you are subscribed to the Google Groups
>> "Clojure" group.
>> To unsubscribe from this group and stop receiving emails from it, send an
>> email to clojure+unsubscr...@googlegroups.com.
>> For more options, visit https://groups.google.com/groups/opt_out.
>>
>>
>
> -- 

-- 
Phillip Lord,                           Phone: +44 (0) 191 222 7827
Lecturer in Bioinformatics,             Email: phillip.l...@newcastle.ac.uk
School of Computing Science,            
http://homepages.cs.ncl.ac.uk/phillip.lord
Room 914 Claremont Tower,               skype: russet_apples
Newcastle University,                   twitter: phillord
NE1 7RU                                 

-- 
-- 
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
--- 
You received this message because you are subscribed to the Google Groups 
"Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to clojure+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.


Reply via email to