clojure - 如何使用 clojure.edn/read 获取文件中的对象序列?

标签 clojure edn

Clojure 1.5 引入 clojure.edn ,其中包括一个需要 PushbackReader 的读取函数.

如果我想读取前五个对象,我可以这样做:

(with-open [infile (java.io.PushbackReader. (clojure.java.io/reader "foo.txt"))]
  (binding [*in* infile]
    (let [edn-seq (repeatedly clojure.edn/read)]
      (dorun (take 5 (map println edn-seq))))))

我怎样才能打印出所有的对象?考虑到其中一些可能为零,似乎我需要检查 EOF 或类似的东西。我想要一个类似于我从 line-seq 得到的对象序列.

最佳答案

使用 :eof 键

http://clojure.github.com/clojure/clojure.edn-api.html

opts is a map that can include the following keys: :eof - value to return on end-of-file. When not supplied, eof throws an exception.



编辑:对不起,这还不够详细!给你:
(with-open [in (java.io.PushbackReader. (clojure.java.io/reader "foo.txt"))]
  (let [edn-seq (repeatedly (partial edn/read {:eof :theend} in))]
    (dorun (map println (take-while (partial not= :theend) edn-seq)))))

应该这样做

关于clojure - 如何使用 clojure.edn/read 获取文件中的对象序列?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15234880/

相关文章:

haskell - 是否可以在 Clojure 中从 Haskell 执行 IO monad?

java - Clojure写文件速度慢

clojure - 在 Clojure/EDN 中序列化排序的映射?

clojure - 修改 clojure 代码时有没有保留注释的好方法?

clojure - 如何在不发出ajax请求的情况下将edn从clojure传递到clojurescript(即通过hiccup生成的页面)

Clojure 头保留在剂量中,运行!循环

database - 函数式编程和业务领域模型

json - 在 Clojure 中使用 Amazonica 时,应该如何格式化 Kinesis 事件的数据?

clojure - yogthos/config 在 CIDER 中获取我的环境,但在 lein with-profile 中运行时则不然

Clojure RuntimeException - 标签 db/id 没有读取器功能