Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use clojure.edn/read to get a sequence of objects in a file?

Tags:

clojure

edn

Clojure 1.5 introduced clojure.edn, which includes a read function that requires a PushbackReader.

If I want to read the first five objects, I can do:

(with-open [infile (java.io.PushbackReader. (clojure.java.io/reader "foo.txt"))]
  (binding [*in* infile]
    (let [edn-seq (repeatedly clojure.edn/read)]
      (dorun (take 5 (map println edn-seq))))))

How can I instead print out all of the objects? Considering that some of them may be nils, it seems like I need to check for the EOF, or something similar. I want to have a sequence of objects similar to what I would get from line-seq.

like image 646
ToBeReplaced Avatar asked Mar 05 '13 21:03

ToBeReplaced


2 Answers

Use :eof key

http://clojure.github.com/clojure/clojure.edn-api.html

opts is a map that can include the following keys: :eof - value to return on end-of-file. When not supplied, eof throws an exception.

edit: sorry, that wasn't enough detail! here y'go:

(with-open [in (java.io.PushbackReader. (clojure.java.io/reader "foo.txt"))]
  (let [edn-seq (repeatedly (partial edn/read {:eof :theend} in))]
    (dorun (map println (take-while (partial not= :theend) edn-seq)))))

that should do it

like image 197
Hendekagon Avatar answered Oct 31 '22 10:10

Hendekagon


I looked at this again. Here is what I came up with:

(defn edn-seq
  "Returns the objects from stream as a lazy sequence."
  ([]
     (edn-seq *in*))
  ([stream]
     (edn-seq {} stream))
  ([opts stream]
     (lazy-seq (cons (clojure.edn/read opts stream) (edn-seq opts stream)))))

(defn swallow-eof
  "Ignore an EOF exception raised when consuming seq."
  [seq]
  (-> (try
        (cons (first seq) (swallow-eof (rest seq)))
        (catch java.lang.RuntimeException e
          (when-not (= (.getMessage e) "EOF while reading")
            (throw e))))
      lazy-seq))

(with-open [stream (java.io.PushbackReader. (clojure.java.io/reader "foo.txt"))]
  (dorun (map println (swallow-eof (edn-seq stream)))))

edn-seq has the same signature as clojure.edn/read, and preserves all of the existing behavior, which I think is important given that people may use the :eof option in different ways. A separate function to contain the EOF exception seemed like a better choice, though I'm not sure how best to capture it since it shows up just as a java.lang.RuntimeException.

like image 45
ToBeReplaced Avatar answered Oct 31 '22 10:10

ToBeReplaced