Julia: show function body (find lost code)

In R-language, I can declare a function and see the body of the function as follows:

> megafoobar = function(x){ return(x + 10000 )} > body(megafoobar) { return(x + 10000) } 

Is this possible in Julia? I wrote a function that was very useful, and it is still in memory / called, but I forgot how I wrote it. I hope this method exists in Julia, so I can find out how I wrote it.

+8
function r julia-lang
source share
1 answer

For functions defined in the package, you can use less or @less . The first takes the name of the function (and returns the first definition, which does not have to be the one you need), the last, the function call.

 less(less) # First definition of less, # with signature (String,Integer) @less less(less) # Definition of less(f::Callable) 

But this will not work with functions that you defined yourself in REPL. You can use code_typed for them, but it only returns the AST (abstract syntax tree) of your code, which is less readable. You also need to specify the type of arguments, because there can be several functions with the same name: you can get them using methods .

 f(x::Number) = x + 1 f(x::AbstractArray) = length(x) methods(f) # 2 methods for generic function "f": # f(x::Number) at none:1 # f(x::AbstractArray{T,N}) at none:1 code_typed(f,(Number,)) # Give the argument types as a tuple # 1-element Array{Any,1}: # :($(Expr(:lambda, {:x}, {{},{{:x,Number,0}},{}}, :(begin # none, line 1: # return x::Number + 1 # end)))) 
+8
source share

All Articles