Data types
Data structure reference: https://juliacollections.github.io/DataStructures.jl/stable/
Array
create new array
a = Int64[]or initialize directly
a = [] >> create new empty arraya = [1,2,3] >> create array of integerscreate an array from 1 to 10
a = collect(1:10)print(a) >> [1,2,3,4,5,6,7,8,9,10]create array of arrays (Vector)
vector = (Array{Int64, 1})[]add to array
a = [1,2,3]push!(a, 5) >> [1,2,3,5]add to Vector
vector = (Array{Int64, 1})[]push!(vector, [1,2,3], [25,26,27) >> [[1,2,3], [25,26,27]]Dictionary
d = Dict(1=>"one", 2=>"two")create with explicity type
d = Dict{Int64, String}()d[0] = "joe"
d[1] = "mary"
check if dict has key
print(haskey(d, 1)) >> trueprint dict values
print(values(d)) >> ["joe", "mary"]loop thru Dict
for k in sort(collect(keys(d))) print(k, ": ", d[k])endStructs
mutable struct Person name::String male::Bool age::Int children::Intendp = Person("Julia", false, 23, 3)people = Person[]push!(people, Person("Joe", true, 30, 2))push!(people, Person("Mary", false, 18, 0))Utilities
get type of variable
typeof(myvar)Map
get square root of each number
a = map((x) -> x^2, [1,2,3,4,5])print(a) >> [1,4,9,16,25]Strings
mystring = "some text"print a string
print(mystring)print("this is a variable here, ", mystring, " within a string")upper case / lower case
print(uppercase(mystring))print(lowercase(mystring))
get substring of string based on letter position
print(show(mystring[4])) >> "e"print(show(mystring[3:8])) >> "me tex"string interpolation (include variable inside string)
a = "welcome"b = "julia"print("$a to $b, everybody") >> "welcome to julia, everybody"run operation inside string
print("1+2 = $(1+2)") >> 1+2=3concat string
s = string("to", "infinity", "and", "beyond")print(s) >> "to infinity and beyond"
replace string
s = "the big black bear"print(replace(s, "black" => "polar")) >> "the big polar bear"replace string with regex
print(replace(s, r"b[\w]*k" => "red")) >> "the big red bear"split string into array
print(split(s)) >> ["the", "big", "black", "bear"]split by delimeter
s = "blue, red, green"print(split(s, ",")) >> ["blue", "red", "green"]combine an array into a String
r = join(collect(1:10), ", ") >> "1,2,3,4..10"Conditional Statements
if / then
if var > 5 var = 0else print("continue")endfor loops
names = ["joe", "bob", "frank"]for name in names print(name)endloop thru a range
x = 0
for k in 1:100
x = x + (1/k)^2
end
nested loop
for i in 1:3, j in 1:3
print("i = ", i, "j=", j, "\n")
end
break out of loop
for i in 1:100
if (i == 5) break end
end
continue next iteration
for i in [1,2,3]
if (i == 5) continue end
i = i + 2
end
DD
Packages
install a new package into ~/.julia
import Pkg
Pkg.add("LinearAlgebra")
to use a pkg
using LinearAlgebrainstall from REPL (cmd line)
julia
enter "]" key to get into pkg console
pkg> add <Package name>
pkg> rm <Package name>
pkg> update <pkg name> # or just "update" to update all pkgs
create a virtual env
julia # enter ] to enter pkg mode
pkg> generate <project name>or 1 liner
julia -e 'using Pkg; Pkg.generate("my project name")'activate virtualenv
cd to project root andjulia --project=.
now install packages within virtual env
julia, ]pkg>add <pkg name>
force project to use pinned version of dependency
julia> Pkg.add("Pipe", preserve=PRESERVE_DIRECT)or create a packages.jl file and execute to install
using Pkg
dependencies = ["IJulia", "Genie"]
Pkg.add(dependencies)
DD
Logging
levels: warn, info, error, debug
@warn "test warning"
DD
DD