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

我的未来我决定 提交于 2019-12-23 16:43:48

问题


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.


回答1:


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



回答2:


I usually wrap stuff in a list,

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


来源:https://stackoverflow.com/questions/6840425/with-clojure-read-read-string-function-how-do-i-read-in-a-clj-file-to-a-list-o

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!