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 Cookbook

You're reading from   Julia 1.0 Programming Cookbook Over 100 numerical and distributed computing recipes for your daily data science work?ow

Arrow left icon
Product type Paperback
Published in Nov 2018
Publisher Packt
ISBN-13 9781788998369
Length 460 pages
Edition 1st Edition
Languages
Tools
Arrow right icon
Authors (2):
Arrow left icon
Przemysław Szufel Przemysław Szufel
Author Profile Icon Przemysław Szufel
Przemysław Szufel
Bogumił Kamiński Bogumił Kamiński
Author Profile Icon Bogumił Kamiński
Bogumił Kamiński
Arrow right icon
View More author details
Toc

Table of Contents (12) Chapters Close

Preface 1. Installing and Setting Up Julia FREE CHAPTER 2. Data Structures and Algorithms 3. Data Engineering in Julia 4. Numerical Computing with Julia 5. Variables, Types, and Functions 6. Metaprogramming and Advanced Typing 7. Handling Analytical Data 8. Julia Workflow 9. Data Science 10. Distributed Computing 11. Other Books You May Enjoy

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 an example.jl file containing this:

println("An example was run!")

We will run this script in this recipe.

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.jl file 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 include will execute the commands given in the example.jl file. At this point, you would probably like to understand what function the include command 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.

You have been reading a chapter from
Julia 1.0 Programming Cookbook
Published in: Nov 2018
Publisher: Packt
ISBN-13: 9781788998369
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