问题
According to the ClojureDocs entry for line-seq (http://clojuredocs.org/clojure_core/clojure.core/line-seq) and the accepted answer for the Stack question (In Clojure 1.3, How to read and write a file), line-seq should return a lazy seq when passed a java.io.BufferedReader.
However when I test this in the REPL, the type is listed as clojure.lang.Cons. See the code below:
=> (ns stack-question
(:require [clojure.java.io :as io]))
nil
=> (type (line-seq (io/reader "test-file.txt")))
clojure.lang.Cons
=> (type (lazy-seq (line-seq (io/reader "test-file.txt"))))
clojure.lang.LazySeq
Wrapping up the line-seq call in a lazy-seq call gives a lazy seq, but according to the docs, this shouldn't be necessary: line-seq should return a lazy seq anyway.
Note: Inside the REPL (I'm using nrepl) it seems that lazy seqs get fully realized, so I thought perhaps it was just a quirk of the REPL; however the same problem exists when I test it with Speclj. Plus, I don't think realizing a lazy seq has to do with what is going on anyway.
EDIT: So I went to check the source code after mobyte's answer said there is a lazy seq in the tail of the cons...
1 (defn line-seq
2 "Returns the lines of text from rdr as a lazy sequence of strings.
3 rdr must implement java.io.BufferedReader."
4 {:added "1.0"}
5 [^java.io.BufferedReader rdr]
6 (when-let [line (.readLine rdr)]
7 (cons line (lazy-seq (line-seq rdr)))))
That call to cons would explain why the type of the return value of line-seq is clojure.lang.Cons.
回答1:
You don't need "wrap" output Cons
because it already has lazy seq as "tail":
(type (line-seq (io/reader "test-file.txt")))
=> clojure.lang.Cons
(type (rest (line-seq (io/reader "test-file.txt"))))
=> clojure.lang.LazySeq
(type (cons 'a (rest (line-seq (io/reader "test-file.txt")))))
=> clojure.lang.Cons
Edit.
Note: Inside the REPL (I'm using nrepl) it seems that lazy seqs get fully realized
Not correct. You can test it:
(with-open [r (io/reader "test-file.txt")] (line-seq r))
=> IOException Stream closed java.io.BufferedReader.ensureOpen (BufferedReader.java:97)
It's because line-seq
returns lazy-seq
which is not fully realized and reader
is already closed when repl tries to realize result later to print it. But if you realize it explicitly it would give normal result without any exceptions:
(with-open [r (io/reader "/home/mobyte/2")] (doall (line-seq r)))
=> ... output ...
来源:https://stackoverflow.com/questions/15182702/why-is-line-seq-returning-clojure-lang-cons-instead-of-clojure-lang-lazyseq