Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Recommended ways to debug Clojure functions?

My current method: if there's a function that I know has an bug, I copy bits and pieces of it into the REPL and evaluate to see if the output is what I expect. To set it up, I have to def the arguments to the function as dummy input. Not terribly time-consuming, but I know there's a more efficient way.

Any suggestions?

like image 869
yayitswei Avatar asked Sep 25 '10 02:09

yayitswei


4 Answers

Does this macro help? It turns a let into a series of defs, so that you can evaluate the subexpressions:

(defmacro def-let
  "like let, but binds the expressions globally."
  [bindings & more]
  (let [let-expr (macroexpand `(let ~bindings))
        names-values (partition 2 (second let-expr))
        defs   (map #(cons 'def %) names-values)]
    (concat (list 'do) defs more)))

I wrote an explanation here: http://www.learningclojure.com/2010/09/astonishing-macro-of-narayan-singhal.html

like image 120
John Lawrence Aspden Avatar answered Nov 11 '22 01:11

John Lawrence Aspden


After reading up on this, my new favorite method is to add

(swank.core/break)

inside the function and inspect values by pressing 't'. (I'm using swank-clojure)

Source: http://hugoduncan.org/post/2010/swank_clojure_gets_a_break_with_the_local_environment.xhtml

like image 22
yayitswei Avatar answered Nov 11 '22 03:11

yayitswei


for bugs spanning several functions I like the Trace macro for reporting the calls and returns of each function.

like image 1
Arthur Ulfeldt Avatar answered Nov 11 '22 01:11

Arthur Ulfeldt


I wrote an tracing library which can show you what value every element returned.
http://github.com/hozumi/eyewrap

like image 1
Takahiro Hozumi Avatar answered Nov 11 '22 01:11

Takahiro Hozumi