I have a function a defined as
(defn a [] "Hello")
I have another variable which b
(def b "a")
I would like to call the function represented by the string value of 'b', ie 'a' should be called. How do I do that?
I have a function a defined as
(defn a [] "Hello")
I have another variable which b
(def b "a")
I would like to call the function represented by the string value of 'b', ie 'a' should be called. How do I do that?
You need to convert it into a symbol and then resolve it:
user=> ((resolve (symbol b)))
"Hello"
user=> ((-> b symbol resolve))
"Hello"
Just to clarify a little, here is a slightly more verbose solution:
(let [func (-> b symbol resolve)]
(func arg1 arg2 arg3)) ; execute the function
(symbol b) you will get symbol object, and when you execute simply a, you will get an object to which a refer, a function in your case. Calling symbol objects makes sense only for looking up keys in map: ((symbol "a") {'a 1 'b 2}) returns 1. And resolve function does exactly what you need, that is, tries to resolve an object behind the symbol. In fact a and (resolve 'a) expressions are nearly equivalent (except for namespaces, but that's another topic).((resolve (symbol b)) 1 2) and ((-> b symbol resolve) 1 2). In fact the latter is transformed by the macro processor into the former.