Memoize an anonymous function with externally scoped variables
I am trying to memoize a function that takes an anonymous handle with an externally-scoped parameter as input. The problem is that no two anonymous functions are ever the same. Therefore, every time the memoization handle mf is invoked, it executes in its entirety and freshly cache’s the results.
mf=memoize(@subfunc);
A=3;
for i=1:4
mf(@(z)A*z)
end
I know that the Lord punisheth those who use eval(), but I can see no other alternative but to pass the anonymous function in char/string form and use evalin. This works fine when there are no externally-scoped variables. Caching indeed only occurs on the first call:
for i=1:4
mf(‘@(z)3*z’)
end
However, when the anonymous function does contain an externally-scoped variable A, the workaround fails.
A=3;
mf(‘@(z)A*z’)
So two questions,
Why does the workaround fail to find externally scoped variables?
Is there a more robust workaround?
function out = subfunc(argFun)
disp ‘Caching’
if isa(argFun,’function_handle’) %argFun is char or string
fun=argFun;
else
fun=evalin(‘caller’,argFun);
end
out=fun(5);
endI am trying to memoize a function that takes an anonymous handle with an externally-scoped parameter as input. The problem is that no two anonymous functions are ever the same. Therefore, every time the memoization handle mf is invoked, it executes in its entirety and freshly cache’s the results.
mf=memoize(@subfunc);
A=3;
for i=1:4
mf(@(z)A*z)
end
I know that the Lord punisheth those who use eval(), but I can see no other alternative but to pass the anonymous function in char/string form and use evalin. This works fine when there are no externally-scoped variables. Caching indeed only occurs on the first call:
for i=1:4
mf(‘@(z)3*z’)
end
However, when the anonymous function does contain an externally-scoped variable A, the workaround fails.
A=3;
mf(‘@(z)A*z’)
So two questions,
Why does the workaround fail to find externally scoped variables?
Is there a more robust workaround?
function out = subfunc(argFun)
disp ‘Caching’
if isa(argFun,’function_handle’) %argFun is char or string
fun=argFun;
else
fun=evalin(‘caller’,argFun);
end
out=fun(5);
end I am trying to memoize a function that takes an anonymous handle with an externally-scoped parameter as input. The problem is that no two anonymous functions are ever the same. Therefore, every time the memoization handle mf is invoked, it executes in its entirety and freshly cache’s the results.
mf=memoize(@subfunc);
A=3;
for i=1:4
mf(@(z)A*z)
end
I know that the Lord punisheth those who use eval(), but I can see no other alternative but to pass the anonymous function in char/string form and use evalin. This works fine when there are no externally-scoped variables. Caching indeed only occurs on the first call:
for i=1:4
mf(‘@(z)3*z’)
end
However, when the anonymous function does contain an externally-scoped variable A, the workaround fails.
A=3;
mf(‘@(z)A*z’)
So two questions,
Why does the workaround fail to find externally scoped variables?
Is there a more robust workaround?
function out = subfunc(argFun)
disp ‘Caching’
if isa(argFun,’function_handle’) %argFun is char or string
fun=argFun;
else
fun=evalin(‘caller’,argFun);
end
out=fun(5);
end memoize, eval, anonymous functions MATLAB Answers — New Questions









