In my problem, I have a function, say optim
, that takes as one of its arguments another function, say objfunc
, along with some other arguments:
optim(objfunc, args...)
I wish to call this function within another function, let's call it run_expt
, and pass a value for objfunc
that I have set to be a specific method of some other function myfunc
.
So specifically, say if myfunc
took four arguments:
myfunc(a, b, c, d)
and I wanted a specific method for it to pass to optim
, which fixed the values of some of its arguments:
mymethod(a) = myfunc(a, [1, 2, 3], (12.0, 47), false)
and those values were determined within the function run_expt
in a way that was not known ahead of time. What is the best way to achieve this, particularly if the method mymethod
is to be performant when called by optim
as it is to be called many times.
I can see two possible ways of doing this, and I would like to know if there is one that is preferred for some stylistic or performance reason, or if - more likely - there is a better way entirely.
Approach #1 nested function:
function myfunc(a, b, c, d)
...
end
function run_expt(exptargs)
...
mymethod(a) = myfunc(a, [1, 2, 3], (12.0, 47), false)
...
optim(mymethod, args...)
---
end
Approach #2 using another function which returns methods:
function myfunc(a, b, c, d)
...
end
function gen_method(methargs...)
...
mymethod(a) = myfunc(a, [1, 2, 3], (12.0, 47), false)
...
return mymethod
end
function run_expt(exptargs...)
...
mymethod = gen_method(methargs...)
...
optim(mymethod, args...)
---
end
Are there distinct advantages/disadvantages to either approach or is there a better way?