Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to return a clojure function as a string

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

like image 704
Jon Rose Avatar asked Mar 09 '12 18:03

Jon Rose


People also ask

How do you return a function in Clojure?

Functions Returning Functions and Closures Our first function will be called adder . It will take a number, x , as its only argument and return a function. The function returned by adder will also take a single number, a , as its argument and return x + a . The returned function form adder is a closure.

What is FN in Clojure?

Most Clojure code consists primarily of pure functions (no side effects), so invoking with the same inputs yields the same output. defn defines a named function: ;; name params body ;; ----- ------ ------------------- (defn greet [name] (str "Hello, " name) )

What is def Clojure?

def is a special form that associates a symbol (x) in the current namespace with a value (7). This linkage is called a var . In most actual Clojure code, vars should refer to either a constant value or a function, but it's common to define and re-define them for convenience when working at the REPL.

How do you define a list in Clojure?

List is a structure used to store a collection of data items. In Clojure, the List implements the ISeq interface. Lists are created in Clojure by using the list function.


1 Answers

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)

like image 123
mtyaka Avatar answered Sep 19 '22 21:09

mtyaka