10

I want to read first n lines from a file using clojure. Here is my code:

(defn read-nth-line [file]
  (with-open [rdr (reader file)]
    (loop [line-number 0]
      (when (< line-number 20)
            (nth (line-seq rdr) line-number)
            (recur (inc line-number))))))

but when I run

 user=> (read-nth-line "test.txt")

 IndexOutOfBoundsException   clojure.lang.RT.nthFrom (RT.java:871)

I have no idea why I got such an error.

2
  • Does test.txtcontain more than 20 lines?
    – SamWhan
    Apr 12, 2016 at 17:00
  • Can you add the require to your question? I like being able to copy paste code into the REPL: (require '[clojure.java.io :refer [reader]]) Apr 12, 2016 at 18:32

1 Answer 1

20

Your code produces an out-of-bounds error because you call line-seq multiple times on the same reader. If you want to get a number of lines from a reader, you should call line-seq only once, then take the desired number of lines from that sequence:

(require '[clojure.java.io :as io])

(defn lines [n filename]
  (with-open [rdr (io/reader filename)]
    (doall (take n (line-seq rdr)))))

Example:

(run! println (lines 20 "test.txt"))

If test.txt contains fewer than 20 lines, this will simply print all the lines in the file.

2
  • Can you add the require to your answer? I like being able to copy paste code into the REPL (require '[clojure.java.io :refer [reader]]) Apr 12, 2016 at 18:31
  • 1
    @MichielBorkent Sure; I was just trying to mirror the OP's style. I added a require with an alias, because I prefer to use :as over :refer when feasible.
    – Sam Estep
    Apr 12, 2016 at 19:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.