使用clojure read / read-string函数,我如何在.clj文件中读取对象列表
问题描述:
如果我这样做
(read-string (slurp "somefile"))
这将只给出文件中的第一个对象,意思是somefile如下:
This will only give me the first object in the file, meaning if "somefile" is as below:
(a obj) (b obj)
然后我只得到(a obj)作为结果。
Then I only get (a obj) as the result.
如何获得所有对象的列表, b
$ b
How do i get a list of all objects, like this?
((a obj) (b obj))
感谢。
答
(defn read-all
[input]
(let [eof (Object.)]
(take-while #(not= % eof) (repeatedly #(read input false eof)))))