When an anonymous function is assigned to a variable, I wonder if there is a way to retrieve the definition of the anonymous function from the variable.
Consider the following scenario. Let’s say I have an array of anonymous functions:
julia> farray = [x->2x, x->√x, x->x^2]
3-element Array{Function,1}:
#1
#2
#3
Later I retrieve these functions from the array and evaluate them, but one function produces an error:
julia> for f = farray
f(-1)
end
ERROR: DomainError:
Stacktrace:
[1] (::##2#5)(::Int64) at ./REPL[0]:1
[2] macro expansion at ./REPL[1]:2 [inlined]
[3] anonymous at ./<missing>:?
To figure out which function is the culprit, I print out the function information before evaluation:
julia> for f = farray
println("f = $f")
f(-1)
end
f = #1
f = #2
ERROR: DomainError:
Stacktrace:
[1] (::##2#5)(::Int64) at ./REPL[0]:1
[2] macro expansion at ./REPL[2]:3 [inlined]
[3] anonymous at ./<missing>:?
The result shows that the culprit is the anonymous function tagged with #2
, but from this it is difficult to figure out what the function is. Is there a way to retrieve the definition of the function #2
here?