The Fast Track To Julia
The Fast Track To Julia
https://juliadocs.github.io/Julia-Cheat-Sheet/
What is…?
Basics
answer = 42
Assignment x, y, z = 1, [1:10; ], "A string"
x, y = y, x # swap x and y
Constant declaration const DATE_OF_BIRTH = 2012
End-of-line comment i = 1 # This is a comment
Delimited comment #= This is another comment =#
x = y = z = 1 # right-to-left
Chaining 0 < x < 3 # true
5 < x != y < 5 # false
function add_one(i)
Function de"nition return i + 1
end
Insert LaTeX symbols \delta + [Tab]
Operators
Standard libraries
Package management
Numbers
Random Numbers
https://juliadocs.github.io/Julia-Cheat-Sheet/ Página 4 de 17
The Fast Track to Julia 15/09/2019 21(17
Arrays
https://juliadocs.github.io/Julia-Cheat-Sheet/ Página 5 de 17
The Fast Track to Julia 15/09/2019 21(17
Linear Algebra
Conditional if-elseif-else-end
for i in 1:10
Simple for loop println(i)
end
for i in 1:10, j = 1:5
Unnested for loop println(i*j)
end
for (idx, val) in enumerate(arr)
Enumeration println("the $idx-th element is $val")
https://juliadocs.github.io/Julia-Cheat-Sheet/ Página 6 de 17
The Fast Track to Julia 15/09/2019 21(17
Functions
Dictionaries
Sets
Collection functions
map(f, coll) or
map(coll) do elem
Apply f to all elements of collection # do stuff with elem
coll
# must contain return
end
Filter coll for true values of f filter(f, coll)
arr = [f(elem) for elem in
List comprehension coll]
Types
end
struct Point{T <: Real}
x::T
y::T
Parametric type
end
p =Point{Float64}(1,2)
Union types Union{Int, String}
Traverse type hierarchy supertype(TypeName) and subtypes(TypeName)
Default supertype Any
All "elds fieldnames(TypeName)
All "eld types TypeName.types
When a type is de"ned with an inner constructor, the default outer
constructors are not available and have to be de"ned manually if need be.
An inner constructor is best used to check whether the parameters
conform to certain (invariance) conditions. Obviously, these invariants can
be violated by accessing and modifying the "elds directly, unless the type
is de"ned as immutable. The new keyword may be used to create an object
of the same type.
Type parameters are invariant, which means that
Point{Float64} <: Point{Real} is false, even though Float64 <: Real.
Tuple types, on the other hand, are covariant:
Tuple{Float64} <: Tuple{Real}.
The type-inferred form of Julia’s internal representation can be found with
code_typed(). This is useful to identify where Any rather than type-speci"c
native code is generated.
Exceptions
Throw throw(SomeExcep())
SomeExcep
Rethrow current rethrow()
exception
struct NewExcep <: Exception
v::String
end
https://juliadocs.github.io/Julia-Cheat-Sheet/ Página 10 de 17
The Fast Track to Julia 15/09/2019 21(17
end
De"ne NewExcep Base.showerror(io::IO, e::NewExcep) = print(io,
"A problem with $(e.v)!")
throw(NewExcep("x"))
Throw error with error(msg)
msg text
try
# do something potentially iffy
catch ex
if isa(ex, SomeExcep)
# handle SomeExcep
elseif isa(ex, AnotherExcep)
Handler # handle AnotherExcep
else
# handle all others
end
finally
# do this in any case
end
Modules
module PackageName
# add module definitions
De"nition # use export to make definitions accessible
end
Include
filename.jl include("filename.jl")
using ModuleName # all exported names
using ModuleName: x, y # only x, y
using ModuleName.x, ModuleName.y: # only x, y
Load import ModuleName # only ModuleName
import ModuleName: x, y # only x, y
import ModuleName.x, ModuleName.y # only x, y
# Get an array of names exported by Module
names(ModuleName)
a new method, but with import Foo.bar, you only need to say
function bar(... and it automatically extends module Foo’s function bar .
Expressions
Macros
macro macroname(expr)
De"nition # do stuff
end
Usage macroname(ex1, ex2, ...) or @macroname ex1, ex2, ...
@test # equal (exact)
@test_approx_eq # equal (modulo numerical errors)
@test x ≈ y # isapprox(x, y)
@assert # assert (unit test)
@which # types used
@time # time and memory statistics
@elapsed # time elapsed
Built-in macros @allocated # memory allocated
@profile # profile
@spawn # run at some worker
@spawnat # run at specified worker
@async # asynchronous task
@distributed # parallel for loop
@everywhere # make available to workers
Rules for creating hygienic macros:
Declare variables inside macro with local .
Do not call eval inside macro.
Escape interpolated expressions to avoid expansion: $(esc(expr))
Parallel Computing
https://juliadocs.github.io/Julia-Cheat-Sheet/ Página 12 de 17
The Fast Track to Julia 15/09/2019 21(17
I/O
stream = stdin
for line in eachline(stream)
Read stream
# do stuff
end
open(filename) do file
for line in eachline(file)
Read "le # do stuff
end
end
https://juliadocs.github.io/Julia-Cheat-Sheet/ Página 13 de 17
The Fast Track to Julia 15/09/2019 21(17
end
using CSV
Read CSV "le data = CSV.read(filename)
using CSV
Write CSV "le CSV.write(filename, data)
using JLD
Save Julia Object save(filename, "object_key", object, ...)
using JLD
Load Julia Object d = load(filename) # Returns a dict of objects
using HDF5
Save HDF5 h5write(filename, "key", object)
using HDF5
Load HDF5 h5read(filename, "key")
DataFrames
end
Type typeof(name)
Type check isa(name, TypeName)
List subtypes subtypes(TypeName)
List supertype supertype(TypeName)
Function methods methods(func)
JIT bytecode code_llvm(expr)
Assembly code code_native(expr)
Many core packages are managed by communities with names of the form
Julia[Topic].
Statistics JuliaStats
Di#erential Equations JuliaDi#Eq (Di#erentialEquations.jl)
Automatic di#erentiation JuliaDi#
Numerical optimization JuliaOpt
Plotting JuliaPlots
Network (Graph) Analysis JuliaGraphs
Web JuliaWeb
Geo-Spatial JuliaGeo
Machine Learning JuliaML
DataFrames # linear/logistic regression
Distributions # Statistical distributions
Flux # Machine learning
Super-used Packages Gadfly # ggplot2-likeplotting
LightGraphs # Network analysis
TextAnalysis # NLP
Naming Conventions
Performance tips
Juno (editor)
JuliaBox (online IJulia notebook)
Jupyter (online IJulia notebook)
Emacs Julia mode (editor)
vim Julia mode (editor)
VS Code extension (editor)
Resources
O$cial documentation .
Learning Julia page.
Month of Julia
Community standards .
Julia: A fresh approach to numerical computing (pdf)
Julia: A Fast Dynamic Language for Technical Computing (pdf)
Videos
https://juliadocs.github.io/Julia-Cheat-Sheet/ Página 17 de 17