Skip to content
Search
Generic filters
Exact matches only

9 Cool Julia Tricks In 4 Minutes. The Best features of the Julia REPL | by DJ Passey | Jul, 2020

The best features of the Julia REPL

DJ Passey
Photo by Filip Baotić on Unsplash

Julia is a new scientific computing language that is as easy to learn as Python, but executes as fast as C. Julia is a compiled language but since it uses a just in time compiler, (like Java), your code can be executed immediately. This feature is put to use in the Julia REPL where you can run lines or blocks of code. REPL stands for read, execute, print, loop.

Once Julia is installed, typing Julia at the command line opens the REPL. The REPL has many features that can help you test snippets and debug your code.

Julia REPL in Package Mode

Type ] at the beginning of the line to enter package mode. This is a shortcut to accessing Julia’s package manager, Pkg. In this mode you can install packages, update them, look at the versions of current packages and more. A few useful commands in package mode are add, remove, build, and update.

Shell Mode

Typing ; at the beginning of a line enters shell mode. This changes the Julia REPL to run bash commands instead of Julia. In shell mode, the REPL acts like a Bash shell and you can enter your standard bash commands like ls, cd, mkdir, and others. I’ve used this feature a lot.

Help Mode

Type ? to enter help mode. In this mode, you can see function or type documentation as well as other tips and hints. When you enter text, Julia will search for documentation and function names that match the text you entered.

julia> uppercase([PRESS TAB]
uppercase(c::T) where T<:AbstractChar in Base.Unicode at strings/unicode.jl:247
uppercase(s::AbstractString) in Base.Unicode at strings/unicode.jl:518

Tab completion works the same way in the REPL as it works in the shell, or in iPython, but it has an additional useful feature. When typing a function name, press tab after the open parenthesis. If you do, Julia will display a list of methods associated with the function name, the types of their arguments and their location in the source code.

julia> @doc max
max(x, y, ...)
Return the maximum of the arguments. See also the maximum function to take the maximum element from a collection.

Calling @doc before a function or type will print the documentation of that object. (In Julia documentation appears in a string before the function definition.) This is useful for quickly finding a function that you need, or reminding yourself about a particular function’s usage.

julia> @time sum(rand(1000));
0.000030 seconds (6 allocations: 8.109 KiB)

The @time macro is another extremely useful tool. As you might expect, it times the execution of the statement it precedes.

julia> apropos("capital")
Base.Unicode.titlecase
Base.Unicode.uppercasefirst

This function accepts a string and searches all documentation for relevant functions and types. This can help avoid digging through documentation to find the functions you need.

julia> methods(uppercase)
# 2 methods for generic function "uppercase":
[1] uppercase(c::T) where T<:AbstractChar in Base.Unicode at strings/unicode.jl:247
[2] uppercase(s::AbstractString) in Base.Unicode at strings/unicode.jl:518

The methods function accepts a function and returns all dispatched definitions of the function, along with the types they accept and where they are found in the source code. This is useful when you are trying to find a function or when you can’t remember to order of arguments.

julia> using Fluxjulia> methodswith(BatchNorm)
[1] show(io::IO, l::BatchNorm) in Flux at /Users/djpassey/.julia/packages/Flux/NpkMm/src/layers/normalise.jl:211
[2] testmode!(m::BatchNorm) in Flux at /Users/djpassey/.julia/packages/Flux/NpkMm/src/layers/normalise.jl:207
[3] testmode!(m::BatchNorm, mode) in Flux at /Users/djpassey/.julia/packages/Flux/NpkMm/src/layers/normalise.jl:207

This is another useful function. This function accepts types and returns all functions that act on that type. In the example above I import a machine learning package, Flux, and call methodswith on the BatchNorm struct. It shows us that there are three functions that accept BatchNorm objects as arguments.

This is useful when you are getting to know a new package and want to learn about the structs defined in the library. It is also helpful when you are looking for a function to act on a certain type.

Conclusion

Overall the Julia REPL can be extremely useful in helping developers locate the right functions and test snippets of code. Hopefully, these tips will come in handy and speed up your Julia development.