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

4
  • possible duplicate of Can you get the "code as data" of a loaded function in Clojure? Commented Mar 9, 2012 at 19:49
  • I think this question has a more googleable title than it's duplicate. Commented Mar 9, 2012 at 19:50
  • You might want to consider marginalia at least for inspiration, see the example marginalia site for an example of what the output looks like. Commented Mar 9, 2012 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. Commented Mar 9, 2012 at 23:46

1 Answer 1

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)

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks a lot. (source) was exactly what I needed! Seems obvious now of course ;)
just kidding, source uses print where as source-fn returns the string I needed. thanks again
Links don't work anymore.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.