I have been struggling to find an answer or develop a solution. I am trying to figure out how to make code that makes code in Clojure. For my first feat, I want a function that will print to stdout the name of the symbol and its value, useful for debugging. Example:
(def mysymbol 5)
(debugging-function mysymbol)
mysymbol: 5
Does that make sense? Thanks for your help.
Post Discussion Update
Here is the answer from @amalloy:
(defmacro ?
"A useful debugging tool when you can't figure out what's going on:
wrap a form with ?, and the form will be printed alongside
its result. The result will still be passed along."
[val]
`(let [x# ~val]
(prn '~val '~'is x#)
x#))
So:
(? myvariable)
You can see a simple version of this that I wrote on github. The main point is that you can't do this with a function, but with a macro it's simple enough - you just have to get your quoting and unquoting right.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With