Book Image

Julia 1.0 Programming Cookbook

By : Bogumił Kamiński, Przemysław Szufel
Book Image

Julia 1.0 Programming Cookbook

By: Bogumił Kamiński, Przemysław Szufel

Overview of this book

Julia, with its dynamic nature and high-performance, provides comparatively minimal time for the development of computational models with easy-to-maintain computational code. This book will be your solution-based guide as it will take you through different programming aspects with Julia. Starting with the new features of Julia 1.0, each recipe addresses a specific problem, providing a solution and explaining how it works. You will work with the powerful Julia tools and data structures along with the most popular Julia packages. You will learn to create vectors, handle variables, and work with functions. You will be introduced to various recipes for numerical computing, distributed computing, and achieving high performance. You will see how to optimize data science programs with parallel computing and memory allocation. We will look into more advanced concepts such as metaprogramming and functional programming. Finally, you will learn how to tackle issues while working with databases and data processing, and will learn about on data science problems, data modeling, data analysis, data manipulation, parallel processing, and cloud computing with Julia. By the end of the book, you will have acquired the skills to work more effectively with your data
Table of Contents (18 chapters)
Title Page
Copyright and Credits
Dedication
About Packt
Contributors
Preface
Index

Useful options for interaction with Julia


Julia has powerful functionalities built into its console that make your daily workflow more efficient. In this recipe, we will investigate some useful options in an interactive session.

Getting ready

Create anexample.jlfile containing this:

println("An example was run!")

We will run this script in this recipe.

 

Note

In the GitHub repository for this recipe you will find the commands.txt file that contains the presented sequence of shell and Julia commands and the example.jl file described above.

Now open your favorite terminal to execute the commands.

How to do it...

We will learn how to work interactively with Julia by going through the following steps:

  1. Start the Julia command line.
  2. Execute two commands in the Julia command line:
julia> x = 10 # just a test command
10

julia> @edit sin(1.0)

After running these commands, an editor with the location of a code section containing the sin function opens. We explained earlier how to choose the editor that Julia uses in the How to customize Julia on startup recipe.

  1. Close the editor to get back to Julia.
  2. Now press Ctrl + L. You will notice that the screen was redrawn and the output from previous commands was cleared.

Now, let us check if example.jl is in our current working directory.

  1. Press ; key and the prompt in Julia should change to this:
shell>
  1. Type ls if you are on Linux or dir in Windows, to execute the shell command. You should get a list of files in your current working directory and after this command, Julia comes back to a standard prompt. When you are sure you have the example.jlfile in your working directory, we can continue.
  1. Start by typing inc in the Julia console:
julia> inc

 

  1. Press Tab. Julia will autocomplete it to include, a built-in function in Julia:
julia> include
  1. Next, continue by entering the text ("exa in the Julia console:
julia> include("exa
  1. Press Tab again to get the following:
julia> include("example.jl"
  1. Finally, type ) and hit Enter. Running includewill execute the commands given in the example.jlfile. At this point, you would probably like to understand what function the includecommand performs.
  1. Press ? in Julia REPL to switch to help mode. The prompt will change to the following:
help?>
  1. Start writing the command you want to check by pressing in:
help?> in
  1. Next, press Tab twice to get the following:
help?> in
in include_string indexin indmax init_worker interrupt inv invoke
include ind2chr indexpids indmin insert! intersect invdigamma invperm
include_dependency ind2sub indices info instances intersect! invmod
help?> in

This time we see that there are multiple commands matching the in pattern and Julia lists them all (this is the reason that the Tab key had to be pressed twice).

  1. Press c and press Tab—now there is only one feasible completion that is filled.
  2. Press Enter to get the following:
help?> include
search: include include_string include_dependency

  include(path::AbstractString)

  Evaluate the contents of the input source file in the global scope of the
  containing module. Every module (except those defined with baremodule) has its
  own 1-argument definition of include, which evaluates the file in that module.
  Returns the result of the last evaluated expression of the input file. During
  including, a task-local include path is set to the directory containing the file.
  Nested calls to include will search relative to that path. This function is
  typically used to load source interactively, or to combine files in packages that
  are broken into multiple source files.

  Use Base.include to evaluate a file into another module.

And we understand exactly what include does. Now, what if we wanted to run the x = 10 command again (this is mostly useful for longer and complex commands in practice)?

  1. Press Ctrl + R to switch Julia into reverse search mode and type x = to get the following:
(reverse-i-search)`x =': x = 10
  1. Press Enter to have the command you found inserted into the Julia prompt:
julia> x = 10
  1. Press Enter to execute the command. Alternatively, we could use arrow up/down or page up/down to traverse the command history.
  1. Finally, to terminate Julia, you can either press Ctrl + D or run the exit() function.

How it works...

Julia REPL offers you several modes, of which the most commonly used are these:

  • Julian: For the execution of Julia code (this is the default).
  • Help: Started by pressing the ? key. As you proceed, you will find instructions on how to use this mode.
  • Shell: Started by pressing the ; key. In this mode, you can quickly execute a shell command without leaving Julia.
  • Package manager: Started by pressing the ] key. In this mode, you can manage packages installed in your system.
  • Backward search mode, which you can enter using Ctrl + R.

You can find more details about options for interacting with Julia in all those modes at https://docs.julialang.org/en/v1.0/stdlib/REPL/.

As you can observe, Julia is smart enough to perform tab completion in a context-sensitive manner—it understands if you are entering a command or a filename. Command history search is also very useful in interactive work.

In the How to customize Julia on startup recipe, we explained how to set up the editor. In this recipe, we saw how you can use the @edit macro to open the location of the definition of the sin function in your chosen editor. Julia recognizes the following editors: Vim, Emacs, gedit, textmate, mate, kate, Sublime Text, atom, Notepad++, and Visual Studio Code. Importantly, @edit recognizes the types of arguments you pass to a function and will show an appropriate method if your chosen editor supports line search on startup (otherwise, an appropriate file will be opened and the line number of the function at hand will be printed in the Julia command line).

There's more...

Apart from the @edit macro, you can use the @less macro or the edit and less functions to see the source code of the function you wish to use (please consult the Julia help guide to understand the detailed differences between them).

If we only want to know the location of a method definition without displaying it, we can use the @which macro:

julia> @which sin(1.0)
sin(x::T) where T<:Union{Float32, Float64} in Base.Math at special/trig.jl:30

See also

The How to customize Julia on startup recipe explains how to use the startup.jl file and how to choose the default Julia editor.