Reputation: 6399
I am trying to write a container function that takes a function name, it's arguments, and calls the function with an additional argument.
I am pretty sure I am doing this wrong, but here's what I've got so far.
(defn get-dyn [& args]
((first args) "some" (rest args)))
So, the first argument is the function name, and that function can have any number of arguments so I guess I have to accept optional parameter?
I need to inject a common argument "some"
into all the functions I call via this function.
so,
(get-dyn str 3 4)
(get-dyn str 32 "willhelm")
etc.. and I would expect it to be equivalent of doing
(str "some" 3 4)
(str "some" 32 "wilhelm")
Except, the (rest args)
returns a set so it doesn't work that way.
I tried doing it with partial
but couldn't work it out.
Is there a way I can solve this problem? or am I better of using induvidual functions?
Upvotes: 0
Views: 1092
Reputation: 862
You can use apply.
user> (defn get-dyn [f & args]
(apply f "some" args))
#'user/get-dyn
user> (get-dyn str 1 2)
"some12"
user> (get-dyn str " " 1 " " 2)
"some 1 2"
Upvotes: 6