On Thu, Dec 15, 2011 at 1:54 PM, Alan Malloy <a...@malloys.org> wrote:
> This will print all the debug information at compile time, which is
> usually not what you want. I have a little macro I use called ?, which
> looks like:
>
> (defmacro ? [x]
>  `(let [x# ~x]
>     (prn '~x '~'is x#)
>     x#))
>
> You could add file and line information to this fairly simply:
>
> (defmacro ? [x]
>  (let [line (:line (meta &form))
>        file *file*]
>    `(let [x# ~x]
>       (println (pr-str '~x) "is" (pr-str x#)
>                (str "; (" ~file ":" ~line ")"))
>       x#)))
>
> And use it like so:
>
> user> (let [x 10]
>        (+ 5 (? x)))
> x is 10 ; (NO_SOURCE_FILE:1)
> 15

You might want to change

(pr-str x#)

to something like

(pr-str
  (if (seq? x#)
    (let [s (Object.)]
      (if (= (nth x# 11 s) s)
        x#
        (concat (take 10 x#) ['...])))
    x#))

which will print sequences longer than ten elements as (1 2 3 4 5 6 7
8 9 10 ...) and prevent infinite seqs from blowing things up. Or more
generally you might want to go through pprint which lets you set
*print-len* and *print-level* to generally control the printing of
very large and/or deeply-nested structures. But that adds a new
dependency that needs to be required anywhere you use the macro ...

-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en

Reply via email to