If I use
(-> "<file>.clj"
(slurp)
(read-string))
That will only read the first form in the file (typically the ns declaration). Is there a way to retrieve a list of forms from the file?
I'd prefer if no external libraries are used.
This function opens a stream for Clojure to read from, and eagerly reads all forms from that stream until read throws an exception (this happens if there is a parse error, or there are no more forms to read).
(import '[java.io PushbackReader])
(require '[clojure.java.io :as io])
(defn read-all
[file]
(let [rdr (-> file io/file io/reader PushbackReader.)]
(loop [forms []]
(let [form (try (read rdr) (catch Exception e nil))]
(if form
(recur (conj forms form))
forms)))))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With