Reputation: 714
What is the most idiomatic way to write a data structure to disk in Clojure, so I can read it back with edn/read? I tried the following, as recommended in the Clojure cookbook:
(with-open [w (clojure.java.io/writer "data.clj")]
(binding [*out* w]
(pr large-data-structure)))
However, this will only write the first 100 items, followed by "...". I tried (prn (doall large-data-structure))
as well, yielding the same result.
I've managed to do it by writing line by line with (doseq [i large-data-structure] (pr i))
, but then I have to manually add the parens at the beginning and end of the sequence to get the desired result.
Upvotes: 6
Views: 1583
Reputation: 22258
You can control the number of items in a collection that are printed via *print-length*
Consider using spit instead of manually opening the writer and pr-str instead of manually binding to *out*
.
(binding [*print-length* false]
(spit "data.clj" (pr-str large-data-structure))
Edit from comment:
(with-open [w (clojure.java.io/writer "data.clj")]
(binding [*print-length* false
*out* w]
(pr large-data-structure)))
Note: *print-length*
has a root binding of nil
so you should not need to bind it in the example above. I would check the current binding at the time of your original pr
call.
Upvotes: 5