For reference, here's a version I wrote while I was working on cl-
format and the pretty printer:
(defn prerr [& args]
"Println to *err*"
(binding [*out* *err*]
(apply println args)))
(defmacro prlabel [prefix arg & more-args]
"Print args to *err* in name = value format"
(cons 'prerr (cons (list 'quote prefix) (mapcat #(list (list 'quote
%) "=" %)
(cons arg more-
args)))))
It prints a label (which is a string, but the macro quotes it for you
and a set of forms you want to print.
Using the above example, this would be (prlabel foo (+ 1 2)) and it
would print:
foo (+ 1 2) = 3
A real example from the PrettyWriter is:
(prlabel tf? (.getColumn this) (buffer-length tokens))
I make sure the output goes to *err* partially because that just seems
right for trace output and partially because cl-format tends to have
*out* bound at its whim.
Tom
On Mar 24, 8:32 am, Joshua Fox <[email protected]> wrote:
> Eric Rochester has a debug macro, together with a walkthrough of how he
> built it,
> herehttp://writingcoding.blogspot.com/2008/09/stemming-part-19-debugging....
>
> Joshua
>
> On Tue, Mar 24, 2009 at 4:43 PM, Mark Volkmann
> <[email protected]>wrote:
>
>
>
> > I want to write a function or macro that allows me to output the value
> > of an expression without repeating it. For example, I want something
> > like (dump (+ 1 2)) to output "(+ 1 2) = 3".
>
> > This works.
>
> > (defn dump1 [string]
> > (println string "=" (load-string string)))
> > (dump1 "(+ 1 2)")
>
> > Note how I had to put the expression passed to dump1 in quotes to make a
> > string.
>
> > I'm wondering if there is a way to avoid that using a macro. The hard
> > part is printing the expression. The following doesn't work. It
> > outputs "3 = 3".
>
> > (defmacro dump2 [expr]
> > `(let [value# ~expr]
> > (pr ~expr)
> > (println " =" value#)))
> > (dump2 (+ 1 2))
>
> > --
> > R. Mark Volkmann
> > Object Computing, Inc.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Clojure" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
-~----------~----~----~----~------~----~------~--~---