我怎样才能在clojure中打印一个引用列表?

时间:2013-08-04 14:19:43

标签: clojure

给出如下列表:

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

:如何将其转换为格式良好的字符串,输出类似于:

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

2 个答案:

答案 0 :(得分:8)

clojure.pprint/writeclojure.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还不支持代码发送。