ppu in clojure sublimeREPL

时间:2013-12-17 09:10:38

标签: clojure sublimetext2 sublimerepl

有没有办法在clojure sublimeREPL

中使用pprint而不是常规打印

目前我必须像这样包装我的代码:

(clojure.pprint/pprint (for [i (range 10)] {i {:times2 (* i 2) :times3 (* i 3)}}))
=>    ({0 {:times2 0, :times3 0}}
       {1 {:times2 2, :times3 3}}
       {2 {:times2 4, :times3 6}}
       {3 {:times2 6, :times3 9}}
       {4 {:times2 8, :times3 12}}
       {5 {:times2 10, :times3 15}}
       {6 {:times2 12, :times3 18}}
       {7 {:times2 14, :times3 21}}
       {8 {:times2 16, :times3 24}}
       {9 {:times2 18, :times3 27}})

抱歉虚拟的例子

1 个答案:

答案 0 :(得分:2)

您可以使用简单的nrepl middleware执行此操作,例如:

(defn pprint-middleware [h]
  (fn [{:keys [code op] :as msg}]
    (if (and (= op "eval") (not (empty? code)))
      (->> #(str "(clojure.pprint/pprint " % ")")
           (update-in msg [:code])
           h)
      (h msg))))

将其添加到您的repl的最简单方法是在您repl-options中配置project.clj

:repl-options {:nrepl-middleware [nrepl.utils/pprint-middleware]})

以下是完整的project.clj示例:

(defproject sample "0.1.0"
  :dependencies [[org.clojure/clojure "1.4.0"]]
  :repl-options {:nrepl-middleware [sample.utils/pprint-middleware]})