【发布时间】:2022-01-11 09:22:37
【问题描述】:
假设我们需要评估源文件中的多个子句,例如
test.clj
@(def x 1)
@(def y 2)
@(def z 3)
如果我们直接使用clj 或lein repl,则只会显示最后的评估,
user => (load-file "test.clj")
3
我们可以用println包围它们以显示所有它们,
test-with-println.clj
(println @(def x 1))
(println @(def y 2))
(println @(def z 3))
user => (load-file "test-with-println.clj")
1
2
3
nil
有什么更好的替代方法可以避免在源代码中侵入printlns,同时能够打印出 REPL 保护下的所有预期评估?
@Solution
感谢@Sean Corfield 的回答中的tap>,我们可以得到如下期望的结果,
- 将每个想要打印的人发送到
tap>。
test-with-tap.clj
(-> @(def x 1) tap>)
(-> @(def y 2) tap>)
(-> @(def z 3) tap>)
- 在
tap>被add-tap开启之前,REPL 将不会收到任何输出。
user=> (load-file "test-with-tap.clj")
nil
user=> (add-tap @(def out (bound-fn* println)))
nil
user=> (load-file "test-with-tap.clj")
1
2
3
user=> (remove-tap out)
nil
user=> (load-file "test-with-tap.clj")
nil
【问题讨论】:
标签: clojure read-eval-print-loop leiningen