Book Image

Julia 1.0 Programming Complete Reference Guide

By : Ivo Balbaert, Adrian Salceanu
Book Image

Julia 1.0 Programming Complete Reference Guide

By: Ivo Balbaert, Adrian Salceanu

Overview of this book

Julia offers the high productivity and ease of use of Python and R with the lightning-fast speed of C++. There’s never been a better time to learn this language, thanks to its large-scale adoption across a wide range of domains, including fintech, biotech and artificial intelligence (AI). You will begin by learning how to set up a running Julia platform, before exploring its various built-in types. This Learning Path walks you through two important collection types: arrays and matrices. You’ll be taken through how type conversions and promotions work, and in further chapters you'll study how Julia interacts with operating systems and other languages. You’ll also learn about the use of macros, what makes Julia suitable for numerical and scientific computing, and how to run external programs. Once you have grasped the basics, this Learning Path goes on to how to analyze the Iris dataset using DataFrames. While building a web scraper and a web app, you’ll explore the use of functions, methods, and multiple dispatches. In the final chapters, you'll delve into machine learning, where you'll build a book recommender system. By the end of this Learning Path, you’ll be well versed with Julia and have the skills you need to leverage its high speed and efficiency for your applications. This Learning Path includes content from the following Packt products: • Julia 1.0 Programming - Second Edition by Ivo Balbaert • Julia Programming Projects by Adrian Salceanu
Table of Contents (18 chapters)

Working with Julia's REPL

We started with Julia's REPL in the previous section to verify the correctness of the installation by issuing the julia command in a Terminal session. The REPL is Julia's working environment, where you can interact with the just in time (JIT) compiler to test out pieces of code. When satisfied, you can copy and paste this code into a file with a .jl extension, such as program.jl. Alternatively, you can continue to work on this code from within a text editor or an IDE, such as the ones we will point out later in this chapter. After the banner with Julia's logo has appeared, you will get a julia> prompt for the input. To end this session and get to the OS Command Prompt, type Ctrl + D, and hit Enter. To evaluate an expression, type it and press Enter to show the result, as shown in the following screenshot:

Working with the REPL (1)

If, for some reason, you don't need to see the result, end the expression with a ; (semicolon) such as 8 * 5; In both the cases, the resulting value is stored, for convenience, in a variable named ans that can be used in expressions, but only inside the REPL. You can bind a value to a variable by entering an assignment as a = 3. Julia is dynamic, and we don't need to enter a type for a, but we do need to enter a value for the variable so that Julia can infer its type. Using a variable b that is not bound to the a value results in the ERROR: UndefVarError: b not defined message. Strings are delineated by double quotes (" "), as in b = "Julia". The following screenshot illustrates this in the REPL:

Working with the REPL (2)

Previous expressions can be retrieved in the same session by working with the up and down arrow keys. The following key bindings are also handy:

  • To clear or interrupt a current command, press Ctrl + C
  • To clear the screen, press Ctrl + L (variables are kept in memory)

Commands from the previous sessions can still be retrieved, because they are stored (with a timestamp) in a repl_history.jl file (in /home/$USER/.julia/logs on Ubuntu, C:\Users\username\.julia\logs on Windows, or ~/.julia/logs/repl_history on OS X). Ctrl + R (produces a reverse-i-search prompt) searches through these commands. 

Typing ? starts up the help mode (help?>) to give you quick access to Julia's documentation. Information on function names, types, macros, and so on, is given when typing in their names. Alternatively, to get more information on a variable, for example, a, type ?a, and to get more information on a function such as sort, type ?sort. To find all the places where a function such as println is defined or used, type apropos("println"), which gives the following output:

Base.Pair
Base.any
Base.@isdefined
Base.eachindex
Base.all
Base.Generator
Base.Timer

Printf.@sprintf
REPL.TerminalMenus.request

Thus, we can see that it is defined in the Base module, and that it is used in several other functions.

Different complete expressions on the same line have to be separated by a ; (semicolon), and only the last result is shown. You can enter multiline expressions, as shown in the following screenshot. If the shell detects that the statement is syntactically incomplete, it will not attempt to evaluate it. Rather, it will wait for the user to enter additional lines until the multiline statement can be evaluated:

Working with the REPL (3)

A handy autocomplete feature also exists. Type one or more letters, press the Tab key twice, and then a list of functions starting with these letters appears. For example, type so, press the Tab key twice, and then you will get the list as sort sort! sortcols sortperm sortperm! sortrows.

If you start a line with ;, the rest of the line is interpreted as a system shell command (try, for example, ls, cd, mkdir, whoami on Linux). The Backspace key returns to the Julia prompt.

A Julia script can be executed in the REPL by calling it with include. For example, for hello.jl, which contains the println("Hello, Julia World!") statement, the command is as follows:

julia> include("hello.jl") 

The preceding command prints the output as follows:

Hello, Julia World! 

Experiment a bit with different expressions to get a feeling for this environment.