Hello Class!
Introduction to Julia
April 3, 2024
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 just containes a path to the environment. You don’t need to commmit it with Git.
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:
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.
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}) Closest candidates are: abs(::Bool) @ Base bool.jl:153 abs(::Pkg.Resolve.VersionWeight) @ Pkg /opt/homebrew/Cellar/julia/1.10.2/share/julia/stdlib/v1.10/Pkg/src/Resolve/versionweights.jl:32 abs(::Missing) @ Base missing.jl:101 ...
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}) Closest candidates are: *(::Any, ::Any, ::Any, ::Any...) @ Base operators.jl:587 *(::LinearAlgebra.Adjoint{<:Number, <:AbstractVector}, ::AbstractVector{<:Number}) @ LinearAlgebra /opt/homebrew/Cellar/julia/1.10.2/share/julia/stdlib/v1.10/LinearAlgebra/src/adjtrans.jl:462 *(::Union{LinearAlgebra.Adjoint{<:Any, <:StridedMatrix{T}}, LinearAlgebra.Transpose{<:Any, <:StridedMatrix{T}}, StridedMatrix{T}}, ::StridedVector{S}) where {T<:Union{Float32, Float64, ComplexF64, ComplexF32}, S<:Real} @ LinearAlgebra /opt/homebrew/Cellar/julia/1.10.2/share/julia/stdlib/v1.10/LinearAlgebra/src/matmul.jl:50 ...
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.