I looked at that 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 retains all existing behavior, which in my opinion is important, given that people can use the :eof parameter in different ways. The single function that contained the EOF exception seemed to be the best choice, although I'm not sure how best to catch it, as it displays as java.lang.RuntimeException .
source share