Hello Class!
Crash course in Julia
April 2, 2025
Extremely new language.
Development began in 2009. Version 1.0 was launched in 2018.
Developed by a group at MIT: “Why We Created Julia”
Designed to be:
(C is very fast, but hard to read and write)
Julia can be installed with…
brew install julia
You can run a whole file of Julia code by clicking the play arrow.
Or you can run a single line or selection…
Shift+Enter
Both of these will execute the Julia code in a terminal, called a REPL—read-eval-print-loop.
Julia has a built in package and environment manager: Pkg.jl
It has its own REPL, which can be launched from Julia by typing a right square bracket: ]
But all of its commands can also be run from withing Julia.
Pkg.add("DataFrames")
I will show commands should be run in the Pkg REPL with: pkg>
]
Then you can create and activate an environment:
This will create an environment in a new folder, “myenv”.
In Julia, you can install packages both system wide
or in an environment you have created.
Once you have a package added to your environment, you will see two files:
Lists the packages you have installed for this environment.
Lists the
With these two files, Julia can always recreate your environment.
In order to have your VS Code workspace automatically use the correct Julia environment, you have to connect it.
Shift+Cmd+P
), then search “Julia: Change Current Environment”After selecting your environment, VS Code will add a “.vscode/settings.json” file to the folder. This file contains a path to the environment.
Once you have a package installed, you can:
.
to reference functionsJulia uses a single =
for assignment
Math is not that different:
Logic operators are written with double &&
or ||
A basic array is constructed as comma-separated elements in square brackets
Single dimension arrays are referred to as Vectors.
Arrays will default to the most general type at construction.
If even one of the elements is a float at construction:
We cannot add a float to an already made Int Array:
But at construction, arrays can be very flexible on the types included:
Julia will default to a general type “Any” to allow an integer, float, string, and boolean to exist in the same array.
You can be explicit about the type for an array by listing it before the square brackets.
This is most useful when creating an empty array—which defaults to type Any.
Empty array with a type:
You can make…
Tuples are constructed as comma-separated elements in parentheses.
Tuples are immutable; arrays are mutable.
LoadError: MethodError: no method matching setindex!(::Tuple{Int64, String, Float64, Bool}, ::Int64, ::Int64)
The function `setindex!` exists, but no method is defined for this combination of argument types.
MethodError: no method matching setindex!(::Tuple{Int64, String, Float64, Bool}, ::Int64, ::Int64)
The function `setindex!` exists, but no method is defined for this combination of argument types.
Stacktrace:
[1] top-level scope
@ In[29]:1
Julia starts indexing from 1.
Julia is not space sensitive.
A for loop instead ends with an end
keyword
Julia functions are defined with the keyword function
and end
:
For one line functions, you can define them with:
Julia has built in unicode support.
Unicode is a text encoding standard that supports characters from all major writing systems and emojis!
What this is actually incredibly convenient for is writing greek letters for equations:
It is a lot tidier than writing out “beta” and “gamma”,
In VS Code, to get one of these unicode symbols, you type:
\:smiley:
for 😃
Once you start typing \:smi
… you can use tab to autocomplete.
Similarily,
\beta
for β
Julia can vectorize (broadcast) any function, which is incredibly useful.
MethodError: no method matching abs(::Vector{Int64}) The function `abs` exists, but no method is defined for this combination of argument types. Closest candidates are: abs(::Bool) @ Base bool.jl:153 abs(::Missing) @ Base missing.jl:101 abs(::Pkg.Resolve.VersionWeight) @ Pkg /opt/homebrew/Cellar/julia/1.11.3/share/julia/stdlib/v1.11/Pkg/src/Resolve/versionweights.jl:32 ... Stacktrace: [1] top-level scope @ In[39]:1
Julia can vectorize (broadcast) any function, which is incredibly useful.
abs
function to each element, add the .
operator:Broadcasting with the .
operator works for any functions:
By default, Julia won’t multiply two vectors element by element:
MethodError: no method matching *(::Vector{Int64}, ::Vector{Int64}) The function `*` exists, but no method is defined for this combination of argument types. Closest candidates are: *(::Any, ::Any, ::Any, ::Any...) @ Base operators.jl:596 *(::LinearAlgebra.Adjoint{<:Any, <:SparseArrays.CHOLMOD.Sparse}, ::VecOrMat) @ SparseArrays /opt/homebrew/Cellar/julia/1.11.3/share/julia/stdlib/v1.11/SparseArrays/src/solvers/cholmod.jl:1419 *(::LinearAlgebra.Adjoint{<:Number, <:AbstractVector}, ::AbstractVector{<:Number}) @ LinearAlgebra /opt/homebrew/Cellar/julia/1.11.3/share/julia/stdlib/v1.11/LinearAlgebra/src/adjtrans.jl:478 ... Stacktrace: [1] top-level scope @ In[42]:4
By default, Julia won’t multiply two vectors element by element:
Instead, use broadcasting to get this behavior
DataFrames
is a package that implements dataframes in Julia.
First, you need to install DataFrames
, then import it.
And then in your Julia code:
Constructing a dataframe by hand:
In Julia it is convention that functions that modify their inputs always end with a bang: !
All of the data science operations we saw with dplyr
and data.table
are possible with Julia and DataFrames.jl
For reading and writing CSVs, use the CSV.jl
package.
Lots of possible plotting packages in Julia.
Plots.jl
the most usedGadfly.jl
is a grammar of graphics packageUnicodePLots.jl
makes plots in the terminalMakie.jl
is a high performance plotting package (can use GPUs for big plots)AlgebraOfGraphics.jl
is a grammar of graphics package that wraps Makie.jl
PlotlyJS.jl
is a wrapper for PlotlyPLots.jl
Programming languages have to be compiled to machine code before they can be run.
Julia does this “just-in-time” (JIT)—i.e. right before the code is run.
This means that Julia can be very fast, but it also means that the first time you run a function, it will be slow.
The speed advantage comes from compiling code for only the specific types of the inputs.
An example of the disadvantage is the “time to first plot” problem.
] activate .
Gadfly
and RDatasets