Skip to content

clojure.core/line-seq

View this page on ClojureDocs

Type: function Added: Clojure 1.0 Examples: 4 Runnable: 0
([rdr])

Returns the lines of text from rdr as a lazy sequence of strings. rdr must implement java.io.BufferedReader.

Examples

by juergenhoetzel on . May have evaluation errors.
by TimMc on . May have evaluation errors.
by kappa on . May have evaluation errors.
by jimka2001 on . May have evaluation errors.

Note by nxtk

Documentation says that line-seq returns a lazy sequence, however
the actual return value is a Cons cell of the first line to a LazySeq of
the second line. Calling realized? on the Cons cell will fail with an exception.

(cons (.readLine rdr) (lazy-seq (line-seq rdr)))

Example:
(def rdr (BufferedReader. (StringReader. " 1\n 2\n 3")))
(def x (line-seq rdr))
(class x)
=> clojure.lang.Cons

(realized? x)
=> Execution error (ClassCastException) class clojure.lang.Cons cannot be cast to class clojure.lang.IPending

The first line was eagerly realized by the initial call to the line-seq.
(.readLine rdr)
=> " 2"

See also


Content from the matching ClojureDocs page, with authors credited on each contribution.