Hi all,

I recently pulled down the latest Clojure master branch and have noticed
a small change in line-seq's behaviour which breaks some of my code.
The code in question uses line-seq like this:

  (use 'clojure.contrib.duck-streams)
  (with-open [ss (java.net.ServerSocket. 4141)]
    (println (first (line-seq (reader (.accept ss))))))

If I run this then telnet to the listen port and send a single line:

  Trying 127.0.0.1...
  Connected to localhost.
  Escape character is '^]'.
  hi

The above code prints nothing until the *second* line arrives from the
socket, even though that line isn't required to fulfil my (first ...)
request.  It appears this behaviour changed in patch 284ee8aa in an
effort to make line-seq return nil (instead of ()) for an empty lazy
seq.  The line-seq definition currently reads:

  (defn line-seq
    "Returns the lines of text from rdr as a lazy sequence of strings.
    rdr must implement java.io.BufferedReader."
    [#^java.io.BufferedReader rdr]
    (let [line  (. rdr (readLine))]
      (when line
        (lazy-seq (cons line (line-seq rdr))))))

and when my code above is blocked, it's because my call to 'first' has
caused the first recursive call to be made, which immediately blocks on
the now-eagerly-performed readLine for the second line of input.  Taking
the line to be returned out of the lazy-seq seems to fix things for me:

  (defn line-seq
    "Returns the lines of text from rdr as a lazy sequence of strings.
    rdr must implement java.io.BufferedReader."
    [#^java.io.BufferedReader rdr]
    (let [line  (. rdr (readLine))]
      (when line
        (cons line (lazy-seq (line-seq-new rdr))))))

and, I think, still preserves the intention of the original change.
Does that seem reasonable?

Thanks,

Mark

-- 
Mark Triggs
<mark.h.tri...@gmail.com>

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