how to return a clojure function as a string
Asked Answered
H

1

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

Hacienda answered 9/3, 2012 at 18:45 Comment(4)
possible duplicate of Can you get the "code as data" of a loaded function in Clojure?Suffragan
I think this question has a more googleable title than it's duplicate.Suffragan
You might want to consider marginalia at least for inspiration, see the example marginalia site for an example of what the output looks like.Neeley
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.Hacienda
P
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)

Praxis answered 9/3, 2012 at 18:57 Comment(3)
Thanks a lot. (source) was exactly what I needed! Seems obvious now of course ;)Hacienda
just kidding, source uses print where as source-fn returns the string I needed. thanks againHacienda
Links don't work anymore.Rosebud

© 2022 - 2024 — McMap. All rights reserved.