9

is there any way to return a clojure function as a string? I am making some online documentation and I would really like to be able to add code into the html by somehow evaluating a function into text.

thanks

Jon Rose
  • 1,457
  • 1
  • 15
  • 25
  • possible duplicate of [Can you get the "code as data" of a loaded function in Clojure?](http://stackoverflow.com/questions/9217911/can-you-get-the-code-as-data-of-a-loaded-function-in-clojure) – Arthur Ulfeldt Mar 09 '12 at 19:49
  • I think this question has a more googleable title than it's duplicate. – Arthur Ulfeldt Mar 09 '12 at 19:50
  • You might want to consider [marginalia](https://github.com/fogus/marginalia) at least for inspiration, see the [example marginalia site](http://fogus.me/fun/marginalia/) for an example of what the output looks like. – Alex Stoddard Mar 09 '12 at 19:54
  • the problem I had in googling for the answer was that everything I found was about literally returning strings not the code of a function as a string. – Jon Rose Mar 09 '12 at 23:46

1 Answers1

6

Take a look at the source-fn function from the clojure.repl namespace:

Usage: (source-fn x)

Returns a string of the source code for the given symbol, if it can find it. This requires that the symbol resolve to a Var defined in a namespace for which the .clj is in the classpath. Returns nil if it can't find the source. For most REPL usage, 'source' is more convenient.

Example: (source-fn 'filter)

mtyaka
  • 8,670
  • 1
  • 38
  • 40