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