Skip to content

Instantly share code, notes, and snippets.

@28
Last active June 29, 2022 21:09
Show Gist options
  • Save 28/b6b94f84a38867339e6ba84d00fd8ca2 to your computer and use it in GitHub Desktop.
Save 28/b6b94f84a38867339e6ba84d00fd8ca2 to your computer and use it in GitHub Desktop.
A simple way to pretty print Clojure data/code to a file.
(require '[clojure.pprint :as pp]
'[clojure.edn :as edn])
(def collection-to-print {:a 1
:b 2
:c 3
:d [1 2 3 4 5]})
(defn pretty-spit
[file-name collection]
(spit (java.io.File. file-name)
(with-out-str (pp/write collection :dispatch pp/code-dispatch))))
;; write to EDN file
(pretty-spit "test.edn" collection-to-print)
;; try to read written file
(edn/read-string (slurp "test.edn"))
@drewverlee
Copy link

trying to read this using edn/read-string fails fwiw.

@28
Copy link
Author

28 commented Jun 29, 2022

Hi,
It seems that the snippet was not functional at all. :-D
I have rewrotten it a bit and added a part with edn/read-string.
Cheers.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment