Book Image

Getting Started with Julia

By : Ivo Balbaert
Book Image

Getting Started with Julia

By: Ivo Balbaert

Overview of this book

Table of Contents (19 chapters)
Getting Started with Julia
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
The Rationale for Julia
Index

Working with Julia's shell


We started with Julia's shell in the previous section (refer to the preceding two screenshots) to verify the correctness of the installation, by issuing the julia command in a terminal session. The shell or 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 the 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 get a julia> prompt for the input. To end this session, and get to the OS Command Prompt, type CTRL + D or quit(), 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 6 * 7. 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: b not defined message. Strings are delineated by double quotes (""), as in b = "Julia". The following screenshot illustrates these workings with 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 (but variables are kept in memory), press CTRL + L

  • To reset the session so that variables are cleared, enter the command workspace() in the REPL

Commands from the previous sessions can still be retrieved, because they are stored (with a timestamp) in a.julia_history file (in /home/$USER on Ubuntu, c:\Users\username on Windows, or ~/.julia_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 quick access to Julia's documentation. Information on function names, types, macros, and so on, is given when typing in their name. Alternatively, to get more information on a variable a, type help(a), and to get more information on a function such as sort, type help(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.println(x)
Base.enumerate(iter)
Base.cartesianmap(f, dims)

Thus, we can see that it is defined in the Base module, and is used in two 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 multi-line 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 multi-line 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 get the list as: sort sort! sortby sortby! sortcols 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, and so on). 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!") command, 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 some feeling for this environment.