How can I pretty print a PersistentHashMap in Clojure to a string?

后端 未结 4 1590
情歌与酒
情歌与酒 2021-02-03 20:43

How can I pretty print a PersistentHashMap in Clojure to a string? I am looking for something like:

(str (pprint {... hash map here...})

which

4条回答
  •  无人及你
    2021-02-03 21:10

    (let [s (java.io.StringWriter.)]
      (binding [*out* s]
        (clojure.pprint/pprint {:a 10 :b 20}))
      (.toString s))
    

    Edit: Equivalent succinct version:

    (with-out-str (clojure.pprint/pprint {:a 10 :b 20}))
    

提交回复
热议问题