Anonymous Functions

There’s a block of code example that I’m trying to understand here:

using DiffEqGPU, OrdinaryDiffEq
function lorenz(du,u,p,t)
    du[1] = p[1]*(u[2]-u[1])
    du[2] = u[1]*(p[2]-u[3]) - u[2]
    du[3] = u[1]*u[2] - p[3]*u[3]
end

u0 = Float32[1.0;0.0;0.0]
tspan = (0.0f0,100.0f0)
p = [10.0f0,28.0f0,8/3f0]
prob = ODEProblem(lorenz,u0,tspan,p)
prob_func = (prob,i,repeat) -> remake(prob,p=rand(Float32,3).*p)

I’m having trouble interpreting the syntax for prob_func. I think its an anonymous function that is feeding (prob, i, repeat) into ODEproblem. But i is undefined and repeat is a function in julia – how are these being used by remake?

Yes, the syntax (prob,i,repeat) -> remake(prob,p=rand(Float32,3).*p) defines an anonymous function which takes three parameters, prob, i and repeat and uses only the first one of the three. The anonymous function is then bound to the name prob_func and you may as well have defined it directly as prob_func(prob,i,repeat) = remake(prob,p=rand(Float32,3).*p).

i is a parameter to the function, so it doesn’t have to be already defined - just like du and u in lorenz function’s definition don’t have to already exist. They get assigned values when they’re called.

As for repeat, within the context of this function, it gets defined to be the argument value instead. Globally, repeat still refers to the function Base.repeat, but in this local scope, repeat instead refers only to the argument that was passed in.