andy
andy

Reputation: 71

With clojure read/read-string function, how do i read in a .clj file to a list of objects

As titled, If I do

(read-string (slurp "somefile"))

This will only give me the first object in the file, meaning if "somefile" is as below:

(a obj) (b obj)

Then I only get (a obj) as the result.

How do i get a list of all objects, like this?

((a obj) (b obj))

Thanks.

Upvotes: 3

Views: 1772

Answers (2)

Hamza Yerlikaya
Hamza Yerlikaya

Reputation: 49329

I usually wrap stuff in a list,

(read-string (str \( (slurp "somefile")  \)))

Upvotes: 5

kotarak
kotarak

Reputation: 17299

(defn read-all
  [input]
  (let [eof (Object.)]
    (take-while #(not= % eof) (repeatedly #(read input false eof)))))

Upvotes: 10

Related Questions