An alternative to source
(which should be available via clojure.repl/source
when starting a REPL, as of 1.2.0
. If you're working with 1.1.0
or lower, source
is in clojure.contrib.repl-utils
.), for REPL use, instead of looking at functions defined in a .clj
file:
(defmacro defsource
"Similar to clojure.core/defn, but saves the function's definition in the var's
:source meta-data."
{:arglists (:arglists (meta (var defn)))}
[fn-name & defn-stuff]
`(do (defn ~fn-name ~@defn-stuff)
(alter-meta! (var ~fn-name) assoc :source (quote ~&form))
(var ~fn-name)))
(defsource foo [a b] (+ a b))
(:source (meta #'foo))
;; => (defsource foo [a b] (+ a b))
A simple print-definition
:
(defn print-definition [v]
(:source (meta v)))
(print-definition #'foo)
#'
is just a reader macro, expanding from #'foo
to (var foo)
:
(macroexpand '#'reduce)
;; => (var reduce)
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…