murtaza52
murtaza52

Reputation: 47431

calling a function when its name is stored as a string value

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?

Upvotes: 4

Views: 698

Answers (1)

Kyle
Kyle

Reputation: 22258

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

Upvotes: 11

Related Questions