【问题标题】:pprint in clojure sublimeREPLclojure sublimeREPL 中的 pprint
【发布时间】:2014-01-04 23:19:41
【问题描述】:

有没有办法在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}})

对不起,这个假的例子

【问题讨论】:

    标签: clojure sublimetext2 sublimerepl


    【解决方案1】:

    你可以用简单的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 的最简单方法是在您的 project.clj 中配置 repl-options

    :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]})
    

    【讨论】:

    • 愚蠢的问题:我应该在我的项目中把这个函数放在哪里?
    • 我试图将 pprint-middleware 放在我的 "utils" 命名空间中,然后我将 :repl-options {:nrepl-middleware [pprint-middleware]} 修改为 :repl-options { :nrepl-middleware [utils/pprint-middleware]} 但启动 repl 时出现错误
    • 尝试使用lein upgrade 更新您的lein。旧版本不支持repl-options
    • 它似乎没有改变任何东西,这是错误消息:CompilerException java.lang.ClassNotFoundException: reply.exports, compile:(/private/var/folders/3w/l39tm8z56cs2ypk389nv_7lh0000gn/T/form -init5267732350145682329.clj:1:8446) 加载命名空间时出错;回退到用户 NullPointerException clojure.pprint/column-writer/fn--7358 (column_writer.clj:78)
    猜你喜欢
    • 1970-01-01
    • 2014-02-10
    • 2013-11-26
    • 2014-03-27
    • 1970-01-01
    • 1970-01-01
    • 2015-04-24
    • 2014-02-12
    • 2014-07-16
    相关资源
    最近更新 更多