views:

212

answers:

4

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?

+2  A: 

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

yayitswei
+2  A: 

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

John Lawrence Aspden
ooh, that is pretty astonishing!
yayitswei
+1  A: 

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

Arthur Ulfeldt
+1  A: 

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

Takahiro Hozumi
How does this compare with the `dotrace` macro in `clojure.contrib.trace`? They seem to have similar goals/functionality.
intuited
Difference is subtle. dotrace can trace specified function deeply in every function which target function calls. eyewrap traces all elements in target function and shows you shallow results.
Takahiro Hozumi