How to load a string into the current namespace (not the kernel)?

I can load an arbitrary source of Clojure using:

(load-string source)

However, if a namespace was not provided, it loads the code into the namespace clojure.core.

For example, the following code:

(load-string "(defn add [a b] (+ a b))")

defines a function:

#'clojure.core/add

Now, is there a way to load this code into another namespace, preferably the same one in which the function is called load-string?

(Besides preceding the declaration of the namespace before the string sourcebefore evaluating. I know this will solve the problem - I would like to know if there is a preferred way)

+5
source share
1 answer

def , namspace , ns var . ns - var, , load

user> (binding [*ns* (find-ns 'foo)] (load-string "(defn f [] 4)"))
#'foo/f
user> (foo/f)
4
+11

All Articles