Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Julia 1.0 Programming

You're reading from   Julia 1.0 Programming Dynamic and high-performance programming to build fast scientific applications

Arrow left icon
Product type Paperback
Published in Sep 2018
Publisher
ISBN-13 9781788999090
Length 196 pages
Edition 2nd Edition
Languages
Tools
Arrow right icon
Author (1):
Arrow left icon
Ivo Balbaert Ivo Balbaert
Author Profile Icon Ivo Balbaert
Ivo Balbaert
Arrow right icon
View More author details
Toc

Table of Contents (12) Chapters Close

Preface 1. Installing the Julia Platform FREE CHAPTER 2. Variables, Types, and Operations 3. Functions 4. Control Flow 5. Collection Types 6. More on Types, Methods, and Modules 7. Metaprogramming in Julia 8. I/O, Networking, and Parallel Computing 9. Running External Programs 10. The Standard Library and Packages 11. Other Books You May Enjoy

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.

You have been reading a chapter from
Julia 1.0 Programming - Second Edition
Published in: Sep 2018
Publisher:
ISBN-13: 9781788999090
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime
Banner background image