Julia Quick Syntax Reference: A Pocket Guide for Data Science Programming
()
About this ebook
This book provides an introduction that reveals basic Julia structures and syntax; discusses data types, control flow, functions, input/output, exceptions, metaprogramming, performance, and more. Additionally, you'll learn to interface Julia with other programming languages such as R for statistics or Python. You will learn how to use Julia packages for data analysis, numerical optimization and symbolic computation, and how to disseminate your results in dynamic documents or interactive web pages.
In this book, the focus is on providing important information as quickly as possible. It is packed with useful information and is a must-have for any Julia programmer.
What You Will Learn
- Set up the software needed to run Julia and your first Hello World example
- Work with types and the different containers that Julia makes available for rapid application development
- Use vectorized, classical loop-based code, logical operators, and blocks
- Explore Julia functions by looking at arguments, return values, polymorphism, parameters, anonymous functions, and broadcasts
- Build custom structures in Julia
- Interface Julia with other languages such as C/C++, Python, and R
- Program a richer API, modifying the code before it is executed using expressions, symbols, macros, quote blocks, and more
- Maximize your code’s performance
Who This Book Is For
Experienced programmers new to Julia, as well as existing Julia coders new tothe now stable Julia version 1.0 release.
Related to Julia Quick Syntax Reference
Related ebooks
Mastering Prolog Programming: From Basics to Expert Proficiency Rating: 0 out of 5 stars0 ratingsApollo Stories - Space History and Law During the Pioneering Years of the Apollo Space Program Rating: 0 out of 5 stars0 ratingsAxiomatics: Mathematical Thought and High Modernism Rating: 0 out of 5 stars0 ratingsLebesgue Measure and Integration: An Introduction Rating: 0 out of 5 stars0 ratingsFrom Simple IO to Monad Transformers Rating: 2 out of 5 stars2/5Theorems of the 21st Century: Volume I Rating: 0 out of 5 stars0 ratingsFascinating Mathematical People: Interviews and Memoirs Rating: 0 out of 5 stars0 ratingsOffice Hours with a Geometric Group Theorist Rating: 0 out of 5 stars0 ratingsThe Loom of God: Tapestries of Mathematics and Mysticism Rating: 4 out of 5 stars4/5Special Functions: An Introduction to the Classical Functions of Mathematical Physics Rating: 5 out of 5 stars5/5Stochastic Geometry and Its Applications Rating: 4 out of 5 stars4/5The Subjectivity of Scientists and the Bayesian Approach Rating: 0 out of 5 stars0 ratingsThe Game of Probability: Literature and Calculation from Pascal to Kleist Rating: 0 out of 5 stars0 ratingsCommunication Nets: Stochastic Message Flow and Delay Rating: 3 out of 5 stars3/5Science Fiction: Time Travel Rating: 5 out of 5 stars5/5Mathematical Doodlings Rating: 0 out of 5 stars0 ratingsLectures on the Infrared Structure of Gravity and Gauge Theory Rating: 0 out of 5 stars0 ratingsTopics in Harmonic Analysis Related to the Littlewood-Paley Theory Rating: 0 out of 5 stars0 ratingsThe Computability of the World: How Far Can Science Take Us? Rating: 4 out of 5 stars4/5Markov Processes from K. Itô's Perspective Rating: 0 out of 5 stars0 ratingsGroup Theory: Birdtracks, Lie's, and Exceptional Groups Rating: 0 out of 5 stars0 ratingsThe Dawning of Gauge Theory Rating: 0 out of 5 stars0 ratingsDigital Color Image Processing Rating: 0 out of 5 stars0 ratingsPost-Modern Algebra Rating: 3 out of 5 stars3/5Theory of Formal Systems Rating: 4 out of 5 stars4/5Mathematical Conversations: Multicolor Problems, Problems in the Theory of Numbers, and Random Walks Rating: 0 out of 5 stars0 ratingsLiterary Mathematics: Quantitative Theory for Textual Studies Rating: 0 out of 5 stars0 ratingsIntroduction To Lagrangian Dynamics Rating: 0 out of 5 stars0 ratingsAn Interpretive Introduction to Quantum Field Theory Rating: 3 out of 5 stars3/5
Intelligence (AI) & Semantics For You
Nexus: A Brief History of Information Networks from the Stone Age to AI Rating: 4 out of 5 stars4/5Mastering ChatGPT: 21 Prompts Templates for Effortless Writing Rating: 4 out of 5 stars4/5The Coming Wave: Technology, Power, and the Twenty-first Century's Greatest Dilemma Rating: 5 out of 5 stars5/5Summary of Super-Intelligence From Nick Bostrom Rating: 4 out of 5 stars4/5Coding with AI For Dummies Rating: 0 out of 5 stars0 ratingsKiller ChatGPT Prompts: Harness the Power of AI for Success and Profit Rating: 2 out of 5 stars2/5Make Money with ChatGPT: Your Guide to Making Passive Income Online with Ease using AI: AI Wealth Mastery Rating: 0 out of 5 stars0 ratingsArtificial Intelligence: A Guide for Thinking Humans Rating: 4 out of 5 stars4/5The Secrets of ChatGPT Prompt Engineering for Non-Developers Rating: 5 out of 5 stars5/5ChatGPT For Dummies Rating: 4 out of 5 stars4/5ChatGPT For Fiction Writing: AI for Authors Rating: 5 out of 5 stars5/5Creating Online Courses with ChatGPT | A Step-by-Step Guide with Prompt Templates Rating: 4 out of 5 stars4/5ChatGPT Millionaire: Work From Home and Make Money Online, Tons of Business Models to Choose from Rating: 5 out of 5 stars5/5AI Investing For Dummies Rating: 0 out of 5 stars0 ratingsWriting AI Prompts For Dummies Rating: 0 out of 5 stars0 ratings101 Midjourney Prompt Secrets Rating: 3 out of 5 stars3/5A Quickstart Guide To Becoming A ChatGPT Millionaire: The ChatGPT Book For Beginners (Lazy Money Series®) Rating: 4 out of 5 stars4/5The Roadmap to AI Mastery: A Guide to Building and Scaling Projects Rating: 3 out of 5 stars3/5The Instant AI Agency: How to Cash 6 & 7 Figure Checks in the New Digital Gold Rush Without Being A Tech Nerd Rating: 0 out of 5 stars0 ratingsDark Aeon: Transhumanism and the War Against Humanity Rating: 5 out of 5 stars5/53550+ Most Effective ChatGPT Prompts Rating: 0 out of 5 stars0 ratingsMidJourney Magnified: Crafting Visual Magic – The Novice to Pro Playbook Rating: 0 out of 5 stars0 ratings
Reviews for Julia Quick Syntax Reference
0 ratings0 reviews
Book preview
Julia Quick Syntax Reference - Antonello Lobianco
Part ILanguage Core
© Antonello Lobianco 2019
A. LobiancoJulia Quick Syntax Referencehttps://doi.org/10.1007/978-1-4842-5190-4_1
1. Getting Started
Antonello Lobianco¹
(1)
Nancy, France
1.1 Why Julia?
With so many programming languages available, why create yet another one? Why invest the time to learn Julia? Is it worth it?
One of the main arguments in favor of using Julia is that it contributes to improving a trade-off that has long existed in programming—fast coding versus fast execution.
On the one side, Julia allows the developer to code in a dynamic, high-level language similar to Python, R, or MATLAB, interacting with the code and having powerful expressivity (see Chapter 6, for example).
On the other side, with minimum effort, developers can write programs in Julia that run (almost) as fast as programs written in C or FORTRAN.
Wouldn’t it be better, though, to optimize existing languages, with their large number of libraries and established ecosystems, rather than create a new language from scratch?
Well, yes and no. Attempts to improve runtime execution of dynamic languages are numerous. PyPy (https://pypy.org), Cython (https://cython.org), and Numba (https://numba.pydata.org) are three notable examples for the Python programming language. They all clash with one fact: Python (and, in general, all the current dynamic languages) was designed before the recent development of just-in-time (JIT) compilers, and hence it offers features that are not easy to optimize. The optimization tools either fail or require complex workarounds in order to work.
Conversely, Julia has been designed from the ground up to work with JIT compilers, and the language features—and their internal implementations—have been carefully considered in order to provide the programmer with the expected productivity of a modern language, all while respecting the constraints of the compiler. The result is that Julia-compliant code is guaranteed to work with the underlying JIT compiler, producing in the end highly optimized compiled code.
../images/477168_1_En_1_Chapter/477168_1_En_1_Figa_HTML.gif The Shadow Costs of Using a New Language
If it is true that the main costs
of using a new language relate to learning the language and having to abandon useful libraries and comfortable, feature-rich development editors that you are accustomed to, it is also true that in the Julia case these costs are mitigated by several factors:
The language has been designed to syntactically resemble mainstream languages (you’ll see it in this book!). If you already know a programming language, chances are you will be at ease with the Julia syntax.
Julia allows you to easily interface your code with all the major programming languages (see Chapter 7, Interfacing Julia with Other Languages
), hence reusing their huge sets of libraries (when these are not already ported to Julia).
The development environments that are available—e.g., Juno (https://junolab.org), IJulia Jupiter kernel (https://github.com/JuliaLang/IJulia.jl), and VSCode Julia plugin (https://github.com/JuliaEditorSupport/julia-vscode)—are frankly quite cool, with many common features already implemented. They allow you to be productive in Julia from the first time you code with it.
Apart from the breakout in runtime performances from traditional high-level dynamic languages, the fact that Julia was created from scratch means it uses the best, most modern technologies, without concerns over maintaining compatibility with existing code or internal architectures. Some of the features of Julia that you will likely appreciate include built-in Git-based package manager, full code introspection, multiple dispatches, in-core high-level methods for parallel computing, and Unicode characters in variable names (e.g., Greek letters).
Thanks to its computational advantages, Julia has its natural roots in the domain of scientific, high-performance programming, but it is becoming more and more mature as a general purpose programming language. This is why this book does not focus specifically on the mathematical domain, but instead develops a broad set of simple, elementary examples that are accessible even to beginner programmers.
1.2 Installing Julia
Julia code can be run by installing the Julia binaries for your system available in the download section (http://julialang.org/downloads/) of the Julia Project website (https://julialang.org).
The binaries ship with a Julia interpreter console (aka, the REPL
—Read, Eval, Print, Loop), where you can run Julia code in a command-line fashion.
For a better experience, check out an Integrated Development Environment, for example, Juno (http://junolab.org/) an IDE based on the Atom (https://atom.io) text editor, or IJulia (https://github.com/JuliaLang/IJulia.jl), the Julia Jupiter (http://jupyter.org/) backend.
Detailed setup instructions can be found on their respective sites, but in a nutshell, the steps are pretty straightforward.
For Juno:
Install the main Julia binaries first.
Download, install, and open the Atom text editor (https://atom.io).
From within Atom, go to the Settings ➤ Install panel.
Type uber-juno into the search box and press Enter. Click the Install button on the package with the same name.
For IJulia:
Install the main Julia binaries first.
Install the Python-based Jupyter Notebook server using the favorite tools of your OS (e.g., the Package Manager in Linux, the Python spip package manager, or the Anaconda distribution).
From a Julia console, type using Pkg; Pkg.update();Pkg.add(IJulia
);Pkg.build(IJulia
).
The IJulia kernel is now installed. Just start the notebook server and access it using a browser.
You can also choose, at least to start with, not to install Julia at all, and try instead one of the online computing environments that support Julia. For example, JuliaBox (https://juliabox.com/), CoCalc (https://cocalc.com/doc/software-julia.html), Nextjournal (https://nextjournal.com), and Binder (https://mybinder.org).
../images/477168_1_En_1_Chapter/477168_1_En_1_Figb_HTML.gif Some tricks for Juno and IJulia
Juno can:
Enable block selection mode with ../images/477168_1_En_1_Chapter/477168_1_En_1_Figc_HTML.gif .
Run a selection of code by selecting it and either selecting Run Block or typing ../images/477168_1_En_1_Chapter/477168_1_En_1_Figd_HTML.gif on Windows and Linux or ../images/477168_1_En_1_Chapter/477168_1_En_1_Fige_HTML.gif on Mac.
Comment/uncomment a block of code with ../images/477168_1_En_1_Chapter/477168_1_En_1_Figf_HTML.gif (Windows and Linux) or ../images/477168_1_En_1_Chapter/477168_1_En_1_Figg_HTML.gif (Mac).
IJulia:
Check out the many keyboard shortcuts available from Help ➤ Keyboard Shortcuts.
Need to run Julia in a computational environment for a team or a class? Use JupyterHub (https://github.com/jupyterhub/jupyterhub), the multi-user solution based on Jupyter.
1.3 Running Julia
There are many ways to run Julia code, depending on your needs:
1.
Julia can run interactively in a console. Start julia to obtain the REPL console, and then type the commands there (type exit() or use CTRL+D when you are finished).
2.
Create a script, i.e. a text file ending in .jl, and let Julia parse and run it with julia myscript.jl [arg1, arg2,..].Script files can also be run from within the Julia console. Just type include(myscript.jl
).
3.
In Linux or on MacOS, you can instead add at the top of the script the location of the Julia interpreter on your system, preceded by #! and followed by an empty row, e.g. #!/usr/bin/julia (You can find the full path of the Julia interpreter by typing which julia in a console.). Be sure that the file is executable (e.g., chmod +x myscript.jl).
You can then run the script with ./myscript.jl.
4.
Use an Integrated Development Environment (such as those mentioned), open a Julia script, and use the run command specific to the editor.
You can define a global (for all users of the computer) and local (for a single user) Julia file that will be executed at any startup, where you can for example define functions or variables that should always be available. The location of these two files is as follows:
Global Julia startup file: [JULIA_INSTALL_FOLDER]\etc\julia\startup.jl (where JULIA_INSTALL_FOLDER is where Julia is installed)
Local Julia startup file: [USER_HOME_FOLDER]\.julia\config\startup.jl (where USER_HOME_FOLDER is the home folder of the local user, e.g. %HOMEPATH% in Windows and ~ in Linux)
Remember to use the path with forward slashes ( / ) with Linux. Note that the local config folder may not exist. In that case, just create the config folder as a .julia subfolder and start the new startup.jl file there.
../images/477168_1_En_1_Chapter/477168_1_En_1_Figh_HTML.gif Julia keeps all the objects created within the same work session in memory. You may sometimes want to free memory or clean up
your session by deleting no longer needed objects. If you want to do this, just restart the Julia session (you may want to use the trick mentioned at the end of Chapter 3) or use the Revise.jl (https://github.com/timholy/Revise.jl) package for finer control.
You can determine which version of Julia you are using with the versioninfo() option (within a Julia session).
1.4 Miscellaneous Syntax Elements
Julia supports single-line ( # ) and multi-line ( #= [...] =# ) comments. Multi-line comments can be nested and appear anywhere in the line:
println(Some code..
) JULIA
#=
Multiline comment
#= nested multiline comment =#
Still a comment
=#
println(#= A comment in the middle of the line =# This is a code
) # Normal single-line comment
You don’t need to use semicolons to indicate the end of a statement. If they’re used, semicolons will suppress the command output (this is done automatically in scripting mode). If the semicolon is used alone in the REPL, it allows you to switch to the OS command shell prompt in order to launch a system-wide command.
Blocks don’t need to be surrounded by parentheses, but they do require the keyword end at the close.
../images/477168_1_En_1_Chapter/477168_1_En_1_Figi_HTML.gif While indentation doesn’t carry any functional meaning in the language, empty spaces sometimes are important. For example, function calls must uses parentheses with the inputs strictly attached to the function name, e.g.:
println (x) # rise an ERROR TEXT
println(x) # OK
In Julia, variable names can include a subset of Unicode symbols, allowing a variable to be represented, for example, by a Greek letter.
In most Julia development environments (including the console), to type a Greek letter, you use a LaTeX-like syntax. This involves typing \, then the LaTeX name for the symbol (e.g. \alpha for α), and then pressing Tab to confirm. Using LaTeX syntax, you can also add subscripts, superscripts, and decorators.
All the following are valid, if not crazy, variable names: x1, x̃, α, y¹, y(a+b), y2, ../images/477168_1_En_1_Chapter/477168_1_En_1_Figj_HTML.jpg , and ../images/477168_1_En_1_Chapter/477168_1_En_1_Figk_HTML.jpg .
Note, however, that while you can use y2 as a variable name, you can’t use 2y, as the latter is automatically interpreted as 2 * y. Together with Unicode, this greatly simplifies the transposition in computer code of mathematical