gen(1) -> 1
==nested function
function f = adder(a)
#adder returns a stateful adding function that keeps track of the total sum
function a = addon(b)
#addon adds b into the summation with each call, returning the cumulative sum over all calls
a = a+b; #assign to state variable a
end
f = @addon
end
gen = adder(0) #nested function maintains state between calls
gen(1) -> 1
gen(1) -> 2
gen(1) -> 3
==
Is it possible to accomplish the same effect as the nested function in Octave? Despite the maintainers' (reasonable) warnings about code clarity, there are some situations that would really benefit from this pattern. (Apologies if my nested function example has any typos, I don't have access to Matlab to test it)
Thanks,