I was trying to do something I thought would be simple but has turned out to be not so and I think the solution might give me a deeper understanding of clojure so I figured I'd ask here.
I want to be able to run the following from a clojure REPL:
(map #(doc %) v)
where v
is a vector of...something, I'm not sure what. The goal was to print out the doc strings for some sequence of functions, but I'm not sure how to express that vector. I've tried a couple of things: (I'll pick a few functions out at random)
[+ - first set]
['+ '- 'first 'set]
[`+ `- `first `set]
[(var +) (var -) (var first) (var set)]
[`~+ `~- `~first `~set]
None work. I also tried to apply
the doc
function but that doesn't work since doc
is a macro and as such cannot be an argument to apply
. What am I missing?
This function takes the meta data of a var and then gets the doc string out of the meta data. You can map it over a sequence of vars:
or shorter:
This doesn't work for special forms, since they are not referenced by vars.
In Clojure 1.2 you can use the function
print-doc
:like this
#'fisrt
is the same as(var first)
.Note that this will not work for special forms:
(doc def)
works, but(print-doc #
def)` gives:This is because special forms aren't defined in vars. You can see how doc works for them by using
macroexpand-1
:In Clojure 1.3
print-doc
is moved in theclojure.repl
namespace and is private, so you cannot execute it directly. You can enter the ns:As you can see, Clojure 1.3 requires getting the metadata explicitly.
If you need to execute this from a different namespace you'll have to export the function. One way to do so is to define a new function: