According to the ClojureDocs entry for the seq line ( http://clojuredocs.org/clojure_core/clojure.core/line-seq ) and the accepted answer to the Stack question ( In Clojure 1.3, How to read and write a file ), the seq line should return lazy seq when passing java.io.BufferedReader.
However, when I test this in REPL, the type is listed as clojure.lang.Cons. See 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
Ending a line-seq call with a lazy-seq call gives a lazy seq, but according to the docs, this is not necessary: ββline-seq should return a lazy seq anyway.
Note: Inside the REPL (I use nrepl) it seems that lazy seqs are fully implemented, so I thought it was just a REPL quirk; however, the same problem exists when I test it with Speclj. Also, I don't think the implementation of lazy seq is related to what happens anyway.
EDIT: So I went to check the source code after mobyte's answer, said there is a lazy seq at the tail of the minuses ...
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)))))
This cons call will explain why the return type of the -seq string is clojure.lang.Cons.
user1922460
source share