View user variables in Julia

Perhaps this is what I just skipped in the documentation, but how can you view the list of current variables in Julia? For example, in R you can use ls() , which will give you a list of user objects in the current area. Is there an equivalent in Julia?

This is very similar to this question , but it seems that the whos (as well as names ) will list modules and other things that are not user defined. How to simply list variables that have been defined by the user and not exported from other modules?

+5
source share
2 answers

One possible approach is to make a whos variant that restricts the summary of objects in the current module:

 function whos_user(m::Module=current_module()) for v in sort(names(m)) s = string(v) if isdefined(m, v) && summary(eval(m, v)) != "Module" && s != "whos_user" println(s) end end end 

Then if we do

 x = 1 y = "Julia" f(n) = n + 1 whos_user() 

we get

 f x y 

You can also write whos_user to return an array of characters rather than print:

 function whos_user(m::Module=current_module()) v = sort(names(m)) filter(i -> isdefined(m, i) && summary(eval(m, i)) != "Module" && string(i) != "whos_user", v) end 

Then, using the same test code as before, we get the following:

 3-element Array{Symbol,1}: :f :x :y 

If there is no better way to do this, I will accept this answer.

+3
source

Julia has a whos function , akin to MATLAB, for this task.

+1
source

All Articles