在使用repl时,调试Clojure代码的最佳方法是什么?
这里有一个非常好用的宏,可以用于调试复杂的let
表达式:
(defmacro def+
"def with binding (def+ [{:keys [a b d]} {:a 1 :b 2 :d 3}])"
[bindings]
(let [let-expr (macroexpand `(let ~bindings))
vars (filter #(not (.contains (str %) "__"))
(map first (partition 2 (second let-expr))))
def-vars (map (fn [v] `(def ~v ~v)) vars)]
(concat let-expr def-vars)))
...以及一篇解释其使用的文章。
我来自Java并熟悉Eclipse,我喜欢Counterclockwise(Clojure开发的Eclipse插件)提供的功能:http://doc.ccw-ide.org/documentation.html#_debug_clojure_code
def-let 的函数版本,将 let 转换为一系列的 defs。部分功劳归于 这里
(defn def-let [aVec]
(if-not (even? (count aVec))
aVec
(let [aKey (atom "")
counter (atom 0)]
(doseq [item aVec]
(if (even? @counter)
(reset! aKey item)
(intern *ns* (symbol @aKey) (eval item)))
; (prn item)
(swap! counter inc)))))
用法:需要使用引号引用内容,例如
(def-let '[a 1 b 2 c (atom 0)])