Homepage / Notes / Computer Science / Programming Languages / Julia
https://docs.julialang.org/en/v1/base/base/ Fast and computation oriented
Log: println()
Pretty log: display()
Log with colours: printstyled()
https://docs.julialang.org/en/v1/base/io-network/#Base.printstyled
using HTTP
to import packages though import HTTP
works too
...
"splat" operator represents a sequence of arguments
add(xs...) = reduce(+, xs)
add(1, 2, 3)
6
add([1, 2, 3]...)
6
;
is used to separate statements that are on the same line
= 1; b = 2; a + b a
3
https://github.com/shg/ob-julia-vterm.el to use :results output, Suppressor.jl
needs to be installed in Julia environment
ans
returns previous result;
activates shell mode?
activates help mode, type function
to pull up docstring of function, type "function"
for search]
activates Package mode_
to separate digits (example: 1_000
)Convention: snake case (long_variable_name
) is typically used
= 1 x
1
Constant variables can be defined with const
const x = 2
true
/ false
true
true
false
false
Int
or UInt
for unsigned (positive integers only) Comes in Int8
, Int16
, Int32
, Int64
and Int128
sizes Julia will default to the processor's size (32/64-Bit) by default if not specified
Float16
, Float32
, Float64
= 1 + 2im
n + 1 n
2 + 2im
1 // 3 + 1 // 3
2//3
Double quotes for strings:
"Damien"
Damien
Single quotes (apostrophes) for single char:
'd'
d
1 + 1
2
Parse integer from strings
parse(Int64, "1337")
1337
Parse float from strings
parse(Float64, "13.37")
13.37
collect(1:10)
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
min()
returns the minimum from any number of arguments
min(2, 3)
2
min(2, 3, 4)
2
Or using the splat
operator, to expand an array to the arguments:
min([2, 3, 4]...)
2
More efficient way and avoiding the splat operator using minimum()
:
minimum([2, 3, 4])
2
Similar for max()
and maximum()
:
max(2, 3, 4)
4
maximum([2, 3, 4])
4
A useful function is extrema()
that returns a tuple with the min and max
extrema([2, 3, 4])
(2, 4)
extrema(1:99)
(1, 99)
https://docs.julialang.org/en/v1/base/strings/ Double quotes are always used for strings, single quotes are used for chars
= "Damien"
name string("My name is ", name)
My name is Damien
= "Damien"
name "My name is " * name
My name is Damien
= "Damien"
name "My name is $name"
My name is Damien
"1 + 1 = $(1 + 1)"
1 + 1 = 2
split("x,y,z", ",")
SubString{String}["x", "y", "z"]
join(["x", "y", "z"], "\n")
x
y
z
startswith("bonjour", "bon")
true
endswith("bonjour", "soir")
false
contains("hello", "x")
false
Remove leading and trailing characters from str
(by default whitespace characters)
strip(" hello")
hello
Can remove a specific character
strip("hello!", '!')
hello
Or a vector of characters
strip(": hello!", [':', ' ', '!'])
hello
Removes trailing newline
chomp("hello\nworld\n")
hello
world
match(r"value = (.*)", "value = 1337")
RegexMatch("value = 1337", 1="1337")
match(r"value = (.*)", "value = 1337").captures
Union{Nothing, SubString{String}}["1337"]
match(r"value = (.*)", "value = 1337").captures |> first
1337
findfirst("Julia", "I love Julia but Julia doesn't love me")
8:12
findlast("Julia", "I love Julia but Julia doesn't love me")
18:22
findnext("Julia", "I love Julia but Julia doesn't love me", 1)
8:12
findnext("Julia", "I love Julia but Julia doesn't love me", 9)
18:22
findprev("Julia", "I love Julia but Julia doesn't love me", 1)
nothing
findprev("Julia", "I love Julia but Julia doesn't love me", 17)
8:12
replace("hello NAME", "NAME" => "Damien")
hello Damien
Vector
is a 1-dimensional array, Matrix
a 2-dimensional array
Immutable, ordered, fixed-length
1, 2) (
(1, 2)
= (9, 10)
t 1] t[
9
= (origin = "Montreal", destination = "Toronto")
route route.origin
Montreal
:origin] route[
Montreal
= route
(; origin, destination) origin
Montreal
1, 2, 3] [
[1, 2, 3]
1 2 3; 4 5 6] [
[1 2 3; 4 5 6]
hcat([1, 2, 3], [4, 5, 6])
[1 4; 2 5; 3 6]
= [1, 2, 3]
a1 = [4, 5, 6]
a2 [a1 a2]
[1 4; 2 5; 3 6]
= []
array
# pushing an element to an array
push!(array, "element")
# remove last element from array
pop!(array)
# appending another array to an array
append!(array, [1, 2, 3])
Any[1, 2, 3]
length([1, 2, 3])
3
size([1, 2, 3])
(3,)
size([1 2 3; 4 5 6])
(2, 3)
Fill an array with n
zeros:
zeros(5)
[0.0, 0.0, 0.0, 0.0, 0.0]
Fill an array with n
zeros with Type specified:
zeros(Int, 5)
[0, 0, 0, 0, 0]
For 2D/3D… arrays:
zeros(Int, 5, 2)
[0 0; 0 0; 0 0; 0 0; 0 0]
Same thing with ones:
ones(Int, 5)
[1, 1, 1, 1, 1]
And random numbers:
rand(5)
[0.20655599984205453, 0.764336425218688, 0.707929049283852, 0.5403245301033117, 0.5747141117006983]
Using standard Normal:
randn(5)
[1.2611015684267128, 0.5431280372799115, -1.0866220141038392, 0.5592503716247522, -0.19501274806221938]
= [5, 10, 15]
xs 1] xs[
5
= [5 10 15; 20 25 30]
m 2,2] m[
25
Using end
= [5, 10, 15, 20, 25]
xs 3:end] xs[
[15, 20, 25]
By column
= [5 10 15; 20 25 30]
m :,1] m[
[5, 20]
By row
= [5 10 15; 20 25 30]
m 1,:] m[
[5, 10, 15]
vcat([1, 2], [3, 4])
[1, 2, 3, 4]
= [1, 2]
a1 = [3, 4]
a2 [a1; a2]
[1, 2, 3, 4]
filter(x -> x < 5, 1:10)
[1, 2, 3, 4]
^2 for x = 1:10] [x
[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
https://blog.lojic.com/2020/12/26/comprehensions-in-julia.html
'a', 'b', 'c'][end] [
c
3, 2, 1] |> sort [
[1, 2, 3]
sort([Dict("x" => 3, "y" => 'a'), Dict("x" => 2, "y" => 'b'), Dict("x" => 1, "y" => 'c')], by = i -> i["x"])
Dict{String, Any}[Dict("x" => 1, "y" => 'c'), Dict("x" => 2, "y" => 'b'), Dict("x" => 3, "y" => 'a')]
sort([1, 2, 3], rev = true)
[3, 2, 1]
partialsortperm([1, 2, 3, 4, 5, 6], 1:3, rev = true)
3-element view(::Vector{Int64}, 1:3) with eltype Int64:
6
5
4
= [1, 2, 3]
a, b, c b
2
... = [1, 2, 3]
a, b b
[2, 3]
deleteat!([1, 2, 3], 1)
[2, 3]
= [1 2 3; 4 5 6]
matrix |> vec matrix
[1, 4, 2, 5, 3, 6]
= [1, 2, 3, 4]
vector |> permutedims vector
[1 2 3 4]
= [1 3; 2 4]
matrix |> transpose matrix
[1 2; 3 4]
'
seems to be a shorthand for transpose:
= [1 3; 2 4]
matrix ' matrix
[1 2; 3 4]
circshift(1:9, 1)
[9, 1, 2, 3, 4, 5, 6, 7, 8]
circshift(1:9, -1)
[2, 3, 4, 5, 6, 7, 8, 9, 1]
No duplicate elements
Set([1, 2, 3, 3])
Set([2, 3, 1])
= Dict("key" => "value")
dict
"key"] dict[
value
= Dict("key" => "value")
dict
"another_key"] = "another_value"
dict[ dict
Dict{String, String} with 2 entries:
"key" => "value"
"another_key" => "another_value"
https://docs.julialang.org/en/v1/manual/functions/
Basic syntax:
function f(x, y)
+ y
x end
f(2, 3)
5
More terse syntax:
f(x, y) = x + y
f(2, 3)
5
-> x*2 x
#1
function f(x; coef = 2)
* coef
x end
f(4)
8
f(4, coef = 4)
16
1, 2, 3] |> length [
3
https://docs.julialang.org/en/v1/manual/functions/#man-vectorized Applies function to all elements of vector, similar to map()
f(x) = x*2
f.([1, 2, 3])
[2, 4, 6]
Even works on the |>
pipe operator!
"list", "of", "string"] .|> [uppercase, reverse, length] [
Any["LIST", "fo", 6]
Passing functions as arguments to other functions is a powerful technique, but the syntax for it is not always convenient. Such calls are especially awkward to write when the function argument requires multiple lines. As an example, consider calling =[map](https://docs.julialang.org/en/v1/base/collections/#Base.map)= on a function with several cases:
map(x->if x < 0 && iseven(x)
return 0
elseif x == 0
return 1
else
return x
end,
-2, -1, 0, 1, 2]) [
[0, -1, 1, 1, 2]
Julia provides a reserved word =do= for rewriting this code more clearly:
map([-2, -1, 0, 1, 2]) do x
if x < 0 && iseven(x)
return 0
elseif x == 0
return 1
else
return x
end
end
[0, -1, 1, 1, 2]
https://docs.julialang.org/en/v1/manual/functions/#Function-composition-and-piping
if 1 > 2
"1 is larger than 2"
else
"2 is larger than 1"
end
2 is larger than 1
for i in 1:5
println(i)
end
1
2
3
4
5
Works on strings too:
for char in "Damien"
println(char)
end
D
a
m
i
e
n
and Dicts:
for (key, value) in Dict("France" => "Paris", "Germany" => "Berlin", "Canada" => "Ottawa")
println(value)
end
Berlin
Ottawa
Paris
eachindex(['a', 'b', 'c'])
Base.OneTo(3)
= 0
i while i < 5
println(i)
+= 1
i end
0
1
2
3
4
break
stops the loop
= 0
i while i < 10
println(i)
+= 1
i if i == 5
break
end
end
0
1
2
3
4
continue
stops this iteration
for i in 1:5
if i == 3
continue
end
println(i)
end
1
2
4
5
∈
(\in
) / d
(\notin
)1 ∈ [1, 2, 3]
true
1 ∉ [1, 2, 3]
false
⊆
(\subseteq
) / ⊈
(\nsubseteq
) / ⊊
(\subsetneq
)1, 2] ⊆ [1, 2, 3] [
true
1, 2] ⊈ [1, 2, 3] [
false
1, 2] ⊆ [1, 2] [
true
"True" subset, can't be equal
1, 2] ⊊ [1, 2] [
false
|>
vs Composition operator ∘
1:10 |> sum |> sqrt
7.416198487095663
∘ sum)(1:10) (sqrt
7.416198487095663
https://julialang.org/blog/2016/02/iteration/
CartesianIndex(1, 1)
CartesianIndex(1, 1)
CartesianIndex(1, 1):CartesianIndex(3, 3)
CartesianIndex{2}[CartesianIndex(1, 1) CartesianIndex(1, 2) CartesianIndex(1, 3); CartesianIndex(2, 1) CartesianIndex(2, 2) CartesianIndex(2, 3); CartesianIndex(3, 1) CartesianIndex(3, 2) CartesianIndex(3, 3)]
using Package
will make all functions defined "exportable" by Package available to use directly
using Statistics
mean([1,2,3])
2.0
import Package
means you have to type Package.function()
to access each function
import Statistics
Statistics.mean([1,2,3])
2.0
http://docs.juliaplots.org/latest/
using Plots
= 1:20; y = rand(20)
x plot(x, y)
savefig("julia-plots/plot.png")
using UnicodePlots
lineplot(sin, 1:.5:20, width = 50)
┌──────────────────────────────────────────────────┐
1 │⠀⠀⢀⠎⠢⡄⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢰⠊⢇⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⡜⠉⢆⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢠│ sin(x)
│⠀⠀⠈⠀⠀⢣⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢀⠇⠀⠈⡆⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢸⠀⠀⠸⡀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⡜│
│⠀⠀⠀⠀⠀⠘⡄⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⡼⠀⠀⠀⢱⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⡇⠀⠀⠀⢣⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢠⠃│
│⠀⠀⠀⠀⠀⠀⢇⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⡇⠀⠀⠀⠈⡆⠀⠀⠀⠀⠀⠀⠀⠀⠀⢰⠁⠀⠀⠀⢸⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⡸⠀│
│⠀⠀⠀⠀⠀⠀⢸⠀⠀⠀⠀⠀⠀⠀⠀⠀⢸⠀⠀⠀⠀⠀⢳⠀⠀⠀⠀⠀⠀⠀⠀⠀⡞⠀⠀⠀⠀⠀⡇⠀⠀⠀⠀⠀⠀⠀⠀⠀⡇⠀│
│⠀⠀⠀⠀⠀⠀⠈⡆⠀⠀⠀⠀⠀⠀⠀⠀⡎⠀⠀⠀⠀⠀⠸⡀⠀⠀⠀⠀⠀⠀⠀⢀⠇⠀⠀⠀⠀⠀⢱⠀⠀⠀⠀⠀⠀⠀⠀⢸⠀⠀│
│⠀⠀⠀⠀⠀⠀⠀⢳⠀⠀⠀⠀⠀⠀⠀⢀⠇⠀⠀⠀⠀⠀⠀⡇⠀⠀⠀⠀⠀⠀⠀⢸⠀⠀⠀⠀⠀⠀⠘⡄⠀⠀⠀⠀⠀⠀⠀⡜⠀⠀│
f(x) │⠤⠤⠤⠤⠤⠤⠤⠼⡤⠤⠤⠤⠤⠤⠤⢼⠤⠤⠤⠤⠤⠤⠤⢵⠤⠤⠤⠤⠤⠤⠤⡮⠤⠤⠤⠤⠤⠤⠤⡧⠤⠤⠤⠤⠤⠤⠤⡧⠤⠤│
│⠀⠀⠀⠀⠀⠀⠀⠀⡇⠀⠀⠀⠀⠀⠀⡎⠀⠀⠀⠀⠀⠀⠀⠸⡀⠀⠀⠀⠀⠀⢀⠇⠀⠀⠀⠀⠀⠀⠀⢸⠀⠀⠀⠀⠀⠀⢸⠀⠀⠀│
│⠀⠀⠀⠀⠀⠀⠀⠀⢱⠀⠀⠀⠀⠀⢀⠇⠀⠀⠀⠀⠀⠀⠀⠀⡇⠀⠀⠀⠀⠀⢸⠀⠀⠀⠀⠀⠀⠀⠀⠘⡄⠀⠀⠀⠀⠀⡜⠀⠀⠀│
│⠀⠀⠀⠀⠀⠀⠀⠀⠸⡀⠀⠀⠀⠀⢸⠀⠀⠀⠀⠀⠀⠀⠀⠀⢱⠀⠀⠀⠀⠀⡎⠀⠀⠀⠀⠀⠀⠀⠀⠀⡇⠀⠀⠀⠀⢀⠇⠀⠀⠀│
│⠀⠀⠀⠀⠀⠀⠀⠀⠀⡇⠀⠀⠀⠀⡇⠀⠀⠀⠀⠀⠀⠀⠀⠀⠸⡄⠀⠀⠀⢠⠇⠀⠀⠀⠀⠀⠀⠀⠀⠀⢸⠀⠀⠀⠀⢸⠀⠀⠀⠀│
│⠀⠀⠀⠀⠀⠀⠀⠀⠀⢸⠀⠀⠀⢰⠁⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢇⠀⠀⠀⡜⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠈⡆⠀⠀⠀⡇⠀⠀⠀⠀│
│⠀⠀⠀⠀⠀⠀⠀⠀⠀⠈⢇⠀⢀⠇⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠸⡀⠀⢠⠃⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢱⠀⠀⡰⠁⠀⠀⠀⠀│
-1 │⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠘⣄⠼⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠱⣠⠊⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠧⣠⠃⠀⠀⠀⠀⠀│
└──────────────────────────────────────────────────┘
⠀0⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀20⠀
⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀x⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
using Dates
Date("2021-01-01")
2021-01-01
ccall(:clock, Int32, ())
5291220
ccall(:getenv, Cstring, (Cstring,), "SHELL") |> unsafe_string
/bin/zsh
tryundefined_method()
e
catch println(e)
finallyprintln("this gets printed regardless")
end
UndefVarError(:undefined_method)
this gets printed regardless
Adding thing::Type
ensure thing
is of type Type
"Damien"::String
Damien
Typically done inside a function:
concat_string(x::String, y::String)::String = x * y
concat_string
concat_string("Hello, ", "World")
Hello, World
Fails:
concat_string("Hello, ", 9)
read
readchomp
readdlm
@code_llvm
Can be used to view the actual LLVM code generated by the Julia compiler
@code_llvm 1 + 1
; @ int.jl:87 within `+`
define i64 @"julia_+_955"(i64 signext %0, i64 signext %1) #0 {
top:
%2 = add i64 %1, %0
ret i64 %2
}
@test
using Test
@test 1 == 1
Test Passed
Expression: 1 == 1
Evaluated: 1 == 1
Tests can be wrapped in a @testset
https://github.com/JuliaIO/JSON.jl
https://github.com/JuliaWeb/HTTP.jl
https://github.com/JuliaData/CSV.jl
https://github.com/JuliaData/DataFrames.jl
https://news.ycombinator.com/item?id=26538150
https://github.com/alan-turing-institute/MLJ.jl
https://github.com/rcalxrc08/FinancialToolbox.jl
https://github.com/fonsp/Pluto.jl
https://github.com/JuliaGPU/CUDA.jl
https://github.com/FluxML/Flux.jl
https://github.com/JuliaDocs/Documenter.jl
https://github.com/JuliaStats/Distributions.jl
https://github.com/JuliaClimate
https://github.com/JuliaData/JuliaDB.jl
https://github.com/timholy/Revise.jl
https://github.com/PumasAI/SimpleChains.jl
https://weavejl.mpastell.com/stable/#Weave.jl-Scientific-Reports-Using-Julia
https://github.com/ndortega/Oxygen.jl
https://github.com/FedeClaudi/Term.jl
https://github.com/JuliaGPU/Metal.jl
https://learnxinyminutes.com/docs/julia/
https://benlauwens.github.io/ThinkJulia.jl/latest/book.html
https://www.youtube.com/watch?v=BnTYMOOPEzw
https://github.com/ninjaaron/administrative-scripting-with-julia
https://b-lukaszuk.github.io/RJ_BS_eng/