Hi guys,
I have a problem which I think should be fairly easy to solve (I guess) but I don’t know how.
I’ll try to describe it as on-point as possible.
I want to define an array of “formulas” that I want to evaluate at different times.
I tried it with expressions and it worked really well at first sight. Here is a simplified example:
ex1 = :(3 + a^2)
ex2 = :(5 + a)
ex3 = :(12)
arr = [ex1, ex2, ex3]
a=15
eval.(arr)
This is exactly what I need (I love that I can evaluate the whole array with just one .eval
call and get an array back) BUT: it only works with global a
since expressions always get evaluated in global scope.
Now where I want to evaluate this stuff is in a scope where a
is local.
Of course I googled and found the advice “try it with functions”. Apart from the problem that I don’t know how to call a bunch of functions in an array with one call I also wasn’t successful in trying to access local variables.
This simple example (with just one function) doesn’t work:
f = () -> a + b^2 + 20
for a = 1:10
b = a-1
println(f())
end
I get an UndefVarError: b not defined
.
How could I make this work?
Any help is greatly appreciated!