I am looking for an elegant way to re-arrange my code. For developing solvers, what happens is you can have a lot of different options which have the same setup. For example
Since julia functions can modify their arguments, side effects can usually be handled by making whatever you want to modify one of the arguments of the function.
This demo uses anonymous functions to allow your solvers to take different parameters, if you want. I'm not sure it's exactly what you're asking, but if you don't know about this already it might be informative.
using Base.Test
function solver1(data, initialize::Bool)
if initialize
fill!(data, 0)
end
return 1
end
function solver2(data, hellostring)
println(hellostring)
return 2
end
function solver(f)
data = [1,2,3,4]
ret = f(data)
println(sum(data))
return ret
end
@test solver(data->solver1(data, false)) == 1
@test solver(data->solver1(data, true)) == 1
@test solver(data->solver2(data, "hello, world")) == 2
which generates the output
10
0
hello, world
10