Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

clojure

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?

like image 534
murtaza52 Avatar asked Dec 27 '22 18:12

murtaza52


1 Answers

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
like image 149
Kyle Avatar answered Dec 29 '22 08:12

Kyle