5

给定一个类似的列表:

(quote (do (println "Hello") (println "Goodbye")))

:我怎样才能把它变成一个格式很好的字符串,它会输出如下内容:

(do
  (println "Hello") 
  (println "Goodbye")
)
4

2 回答 2

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 ...)

于 2013-08-04T14:28:20.003 回答
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 还不支持代码调度。

于 2013-08-04T23:03:14.087 回答