我需要将 clojure 映射保存到文件中,稍后再读回以处理它们。
这是我能想到的。有没有更好的方法来完成同样的事情?
user=> (def my-data (for [ a [ "Person1" "Person2" ] b [ "Address1" "Address2"] c (range 10) ] {:name a :address b :index c} ))
#'user/my-data
user=> (count my-data)
40
user=> (defn write-data[xs] (with-open [wrtr (clojure.java.io/writer "my-data.txt") ]
(doall (map #(.write wrtr (str % "\n")) xs))))
#'user/write-data
user=> (write-data my-data)
user=> (defn read-data[] (with-open [rdr (clojure.java.io/reader "my-data.txt") ]
(doall (map #(load-string %) (line-seq rdr)))))
#'user/read-data
user=> (count (read-data))
40