li berrouz
li berrouz

Reputation: 43

How I can run functions, when I have only one name?

Example. We have very easy funcs.

(defn func1 []
  (println "i'm func1"))

(defn func2 []
  (println "i'm func2"))

And I create list with names of this functions.

(def listOfFunc '(func1 func2))

How I can run this functions, when I get name of functions from list?

Sorry for my bad english and very noob question.

Upvotes: 1

Views: 86

Answers (1)

kongeor
kongeor

Reputation: 732

Is there a specific reason why these functions are stored in a list?

If no, then you can use a vector which will result into something like this:

(def fns [func1 func2]) 
(map #(%) fns)

Note that this will result into a lazy seq of two nils: (nil nil). If however your functions are only for side-effects, as the ones you listed, then you can wrap them into a dorun:

(dorun (map #(%) fns))

which will return a single nil.

Now, if you still prefer using a list, you will have to resolve your symbols into the corresponding functions. So I guess something like this would work:

(map #((ns-resolve 'foo.core %)) listOfFunc)

where 'foo.core should be replaced with the namespace that has your functions.

Upvotes: 3

Related Questions