给出如下列表:
(quote (do (println "Hello") (println "Goodbye")))
:如何将其转换为格式良好的字符串,输出类似于:
(do
(println "Hello")
(println "Goodbye")
)
答案 0 :(得分:8)
将clojure.pprint/write
与clojure.pprint/code-dispatch
:
(clojure.pprint/write '(do (println "Hello") (println "Goodbye"))
:dispatch clojure.pprint/code-dispatch)
对于少量的代码,你不会得到新的代码。但是像这样的电话
(clojure.pprint/write '(do (println "Hello") (println "Goodbye") (println "Hey, you left me out!"))
:dispatch clojure.pprint/code-dispatch)
返回
=> (do
(println "Hello")
(println "Goodbye")
(println "Hey, you left me out!"))
要将其作为字符串捕获,请将调用包裹在(with-out-str ...)
答案 1 :(得分:5)
作为clojure.pprint
的明显更高效的替代方案,您可能想尝试Brandon Bloom的fipp:
;; add [fipp "0.4.0"] to :dependencies
(require '[fipp.edn :as fipp])
(fipp/pprint '(do (println :foo) (println :bar) (println :quux))
{:width 30}) ; force wrapping despite small size of data
;; printed output:
(do
(println :foo)
(println :bar)
(println :quux))
不幸的是,Fipp还不支持代码发送。