Julia for Analysts: Tips for Better Beginnings - Embrace the REPL (#3)

22 August 2022

TL;DR

Invest time in learning to use REPL. Most importantly learn to get help (docstrings, examples, methods) within REPL to avoid breaking your flow.

Embrace the REPL

It is too powerful not to! And the time invested in mastering it will return a thousand-fold!

What is REPL?

The official documentation says:

Julia comes with a full-featured interactive command-line REPL (read-eval-print loop) built into the julia executable. In addition to allowing quick and easy evaluation of Julia statements, it has a searchable history, tab-completion, many helpful keybindings, and dedicated help and shell modes.

That is an understatement of the century, see Julia REPL Tips and Tricks to learn more.

I will highlight some of my personal favourites in the following sections. I will assume you have enabled OhMyREPL.jl package in startup.jl or just loaded it by using OhMyREPL. You can find a how-to for setting up your startup.jl file in article #1 of this series.

Switching modes

It's a 4-in-1 deal: your standard programming mode and three more, Package, Shell, and Help. Type a leading character to try them:

If you delete the character (eg, with a Backspace), it will jump back to normal Julia mode.

Why is that so powerful? It allows you to quickly switch into a dedicated context(/experience) allowing you to achieve your goals more efficiently. All it takes is one character in front of your command.

With the difference being a single leading character you can also quickly take the same command and use it in different modes (more on that in the next section about Getting Help) without any clicking or copy&pasting.

For example, to active my project, I would:

Why did I write the two commands and not activate with a specific path? I often want to load scripts or data from the same folder and use relative addresses (eg, data_raw/file.csv), which is why I change the working directory first.

Autocompletion, Navigation, and ans

Keep pressing TAB. All relevant packages, variables, functions, etc. are preloaded, so you can save yourself a lot of typing.

Navigating back to your recent commands has never been easier - just use ARROW UP and ARROW DOWN. If you jump to a certain point in history, you can also replay your commands from that point by continuing with ARROW DOWN. Lastly, you can quickly fuzzy search your history with CTRL+r.

Results of the previous command are saved in a variable named ans, so you simply fly through some simple calculations without having to nest multiple function calls or use pipes. Try it!

Getting help without leaving my workflow (no more googling)

With Julia REPL, you can avoid the majority of googling for help, which means more productive coding time!

To be able to run the below examples, you'll need to run using DataFrames, Dates, Plots first.

Different ways how to help yourself:

Was that too many new functions? You need to remember only the first few letters, then press a TAB and REPL will magically autocomplete for you!

Extracting the Command History

You can also pull all your commands to create a proper script: edit(REPL.find_hist_file()). You might need to run import REPL first.

If you use Vim or VSCode, you can then simply remove all the lines starting "#" and your script is ready! WARNING: Do not change the actual file - you would break it!!

Some Random Tips

Example workflow:

Imagine you write a function and get an error

Resources to learn more

CC BY-SA 4.0 Jan Siml. Last modified: December 09, 2024. Website built with Franklin.jl and the Julia asdasdas programming language.