Skip to content
Snippets Groups Projects

Compare revisions

Changes are shown as if the source revision was being merged into the target revision. Learn more about comparing revisions.

Source

Select target project
No results found

Target

Select target project
  • R3/school/courses
  • vilem.ded/courses
  • todor.kondic/courses
  • noua.toukourou/courses
  • nene.barry/courses
  • laurent.heirendt/courses
  • marina.popleteeva/courses
  • jenny.tran/courses
8 results
Show changes
Commits on Source (16)
Showing
with 2194 additions and 1 deletion
......@@ -23,3 +23,4 @@ package.json
./theme/package.json
__pycache__/
contribute.egg-info/
.*.swp
......@@ -2,7 +2,7 @@
- Create your own copy of the original (also called `upstream`) repository (`fork` in `git` jargon)
- Download your copy to your computer (`clone` in `git` jargon)
- Create a new brach
- Create a new branch
- Make changes
- Commit changes
- <font color="red">Push changes to server</font>
......
<div class=leader>
<i class="twa twa-axe"></i><i class="twa twa-carpentry-saw"></i><i class="twa twa-screwdriver"></i><i class="twa twa-wrench"></i><i class="twa twa-hammer"></i><br>
Bootstrapping Julia
</div>
# Installing Julia
Recommended method:
- Download an archive from https://julialang.org/downloads/
- Execute `julia` or `julia.exe` as-is
- Link it to your `$PATH`
Distribution packages usually work well too:
- **Debians&Ubuntus**: `apt install julia`
- **Iris/Aion**: `module add lang/Julia`
# Life in REPL
```julia
user@pc $ julia
julia> sqrt(1+1)
1.4142135623730951
julia> println("Well hello there!")
Well hello there!
julia> ?
help?> sqrt
sqrt(x)
Computes the square root .....
```
# REPL modes
Julia interprets some additional keys to make our life easier:
- `?`: help mode
- `;`: shell mode
- `]`: packaging mode (looks like a box!)
- `Backspace`: quits special mode
- `Tab`: autocomplete anything
- `\`... `Tab`: expand math characters
# Managing packages from the package management environment
- Install a package
```julia
] add UnicodePlots
```
- Uninstall a package
```julia
] remove UnicodePlots
```
# Loading libraries, modules and packages
- Load a local file (with shared functions etc.)
```julia
include("mylibrary.jl")
```
- Load a package, add its exports to the global namespace
```julia
using UnicodePlots
```
# <i class="twa twa-light-bulb"> </i> How to write a standalone program?
*Your scripts should communicate well with the environment!*
(that means, among other, you)
```julia
#!/usr/bin/env julia
function process_file(filename)
@info "Processing $filename..."
# ... do something ...
if error_detected
@error "something terrible has happened"
exit(1)
end
end
for file in ARGS
process_file(file)
end
```
Correct processing of commandline arguments makes your scripts *repurposable*
and *configurable*.
# <i class="twa twa-light-bulb"></i> Workflow: Make a local environment for your script
- Enter a local project with separate package versions
```julia
] activate path/to/project
```
- Install dependencies of the local project
```julia
] instantiate
```
- Execute a script with the project environment
```sh
$ julia --project=path/to/project script.jl
```
(Project data is stored in `Project.toml`, `Manifest.toml`.)
<div class=leader>
<i class="twa twa-rocket"></i>
<i class="twa twa-rocket"></i>
<i class="twa twa-rocket"></i><br>
Parallel Julia
</div>
# Julia model of distributed computation
<center>
<img src="slides/img/distrib.svg" width="50%">
</center>
# Basic parallel processing
**Using `Threads`:**
1. start Julia with parameter `-t N`
2. parallelize (some) loops with `Threads.@threads`
```julia
a = zeros(100000)
Threads.@threads for i = eachindex(a)
a[i] = hardfunction(i)
end
```
**Using `Distributed`:**
```julia
using Distributed
addprocs(N)
newVector = pmap(myFunction, myVector)
```
We will use the `Distributed` approach.
# Managing your workers
```julia
using Distributed
addprocs(4)
myid()
workers()
```
Running commands on workers:
```julia
@spawnat 3 @info "Message from worker"
@spawnat :any myid()
```
Getting results from workers:
```julia
job = @spawnat :any begin sleep(10); return 123+321; end
fetch(job)
```
Cleaning up:
```julia
rmprocs(workers())
```
# Processing lots of data items in parallel
```julia
datafiles = ["file$i.csv" for i=1:20]
@everywhere function process_file(name)
println("Processing file $name")
# ... do something ...
end
pmap(process_file, datafiles)
```
<i class="twa twa-light-bulb"></i><i class="twa twa-light-bulb"></i> Doing it manually:
```julia
@sync for f in datafiles
@async @spawnat :any process_file(f)
end
```
# Gathering results from workers
```julia
items = collect(1:1000)
@everywhere compute_item(i) = 123 + 321*i
pmap(compute_item, items)
```
<i class="twa twa-light-bulb"></i><i class="twa twa-light-bulb"></i><i class="twa twa-light-bulb"></i> Doing manually with `@spawnat`:
```julia
futures = [@spawnat :any compute_item(item) for item in items]
fetch.(futures)
```
# How to design for parallelization?
**Recommended way:** *Utilize the high-level looping primitives!*
- use `map`, parallelize by just switching to `pmap`
- use `reduce` or `mapreduce`, parallelize by just switching to `dmapreduce` (DistributedData.jl)
# <i class="twa twa-light-bulb"></i> Parallel → distributed processing
It is very easy to organize *multiple computers* to work for you!
You need a working `ssh` connection:
```sh
user@pc1 $ ssh server1
Last login: Wed Jan 13 15:29:34 2021 from 2001:a18:....
user@server $ _
```
Spawning remote processes on remote machines:
```julia
julia> using Distributed
julia> addprocs([("server1", 10), ("pc2", 2)])
```
**Benefit:** No additional changes to the parallel programs!
<div class=leader>
<i class="twa twa-abacus"></i>
<i class="twa twa-laptop"></i>
<i class="twa twa-desktop-computer"></i>
<i class="twa twa-flag-luxembourg"></i><br>
Utilizing ULHPC <i class="twa twa-light-bulb"></i>
</div>
# What does the cluster look like? (Iris)
<center>
<img src="slides/img/iris.png" width="30%">
<br>
<tt>hpc-docs.uni.lu/systems/iris</tt>
</center>
# Running Julia on the computing nodes
Start an allocation and connect to it:
```sh
0 [mkratochvil@access1 ~]$ srun -p interactive -t 30 --pty bash -i
```
(You can also use `si`.)
After some brief time, you should get a shell on a compute node. There you can install and start Julia as usual:
```
0 [mkratochvil@iris-131 ~](2696005 1N/T/1CN)$ module add lang/Julia
0 [mkratochvil@iris-131 ~](2696005 1N/T/1CN)$ julia
_
_ _ _(_)_ | Documentation: https://docs.julialang.org
(_) | (_) (_) |
_ _ _| |_ __ _ | Type "?" for help, "]?" for Pkg help.
| | | | | | |/ _` | |
| | |_| | | | (_| | | Version 1.6.2 (2021-07-14)
_/ |\__'_|_|_|\__'_| | Official https://julialang.org/ release
|__/ |
julia>
```
# Making a HPC-compatible Julia script
Main challenges:
1. discover the available resources
2. spawn worker processes at the right place
```julia
using ClusterManagers
addprocs_slurm(parse(Int, ENV["SLURM_NTASKS"]))
# ... continue as usual
```
# Scheduling an analysis script
Normally, you write a "batch script" and add it to a queue using `sbatch`.
Script in `runAnalysis.sbatch`:
```sh
#!/bin/bash
# SBATCH -J MyAnalysisInJulia
# SBATCH -n 10
# SBATCH -c 1
# SBATCH -t 30
# SBATCH --mem-per-cpu 4G
julia runAnalysis.jl
```
You start the script using:
```sh
$ sbatch runAnalysis.sbatch
```
<div class=leader>
<i class="twa twa-blueberries"></i>
<i class="twa twa-red-apple"></i>
<i class="twa twa-melon"></i>
<i class="twa twa-grapes"></i><br>
Questions?
</div>
Lets do some hands-on problem solving (expected around 15 minutes)
This diff is collapsed.
2022/2022-06-08_JuliaForNewcomers/slides/img/favicon.ico

39.9 KiB

2022/2022-06-08_JuliaForNewcomers/slides/img/iris.png

3.37 MiB

<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 153.14 98.64"><defs><style>.cls-1{fill:#1a1a1a}.cls-2{fill:#4d64ae}.cls-3{fill:#ca3c32}.cls-4{fill:#9259a3}.cls-5{fill:#399746}</style></defs><title>Asset 2</title><g id="Layer_2" data-name="Layer 2"><g id="Layer_1-2" data-name="Layer 1"><g id="layer1"><g id="g3855"><g id="g945"><g id="g984"><g id="g920"><path id="path3804" d="M93.14,80.94h-13V21.13l13-3.58Z" class="cls-1"/><g id="g898"><g id="g893"><path id="path19" d="M22.17,36.33a8.9,8.9,0,1,1,8.9-8.9A8.91,8.91,0,0,1,22.17,36.33Z" class="cls-2"/></g><path id="path3819" d="M29.14,80.83A26.48,26.48,0,0,1,27.83,90a12.12,12.12,0,0,1-3.62,5.4A12.33,12.33,0,0,1,18.57,98a36.64,36.64,0,0,1-7.32.67,22.47,22.47,0,0,1-4.81-.47A13,13,0,0,1,2.9,96.93,6,6,0,0,1,.76,95.07,3.62,3.62,0,0,1,0,92.88,4.26,4.26,0,0,1,1.59,89.5a6.47,6.47,0,0,1,4.33-1.35,5,5,0,0,1,1.87.32,6,6,0,0,1,1.43.79,12,12,0,0,1,1.16,1.07c.31.4.59.77.83,1.12A7.58,7.58,0,0,0,12.72,93a2.3,2.3,0,0,0,1.15.4,1.85,1.85,0,0,0,1-.28,2,2,0,0,0,.71-1,7.18,7.18,0,0,0,.4-1.91,23.12,23.12,0,0,0,.16-3.06V40.48l13-3.58Z" class="cls-1"/></g><path id="path3802" d="M48.14,37.94V68a6.14,6.14,0,0,0,.47,2.39A6.45,6.45,0,0,0,50,72.24a7,7,0,0,0,2,1.27,6.12,6.12,0,0,0,2.4.48,4.2,4.2,0,0,0,1.61-.4,8.42,8.42,0,0,0,1.8-1.12,13.27,13.27,0,0,0,1.81-1.66,12.92,12.92,0,0,0,1.61-2.11V37.94h13v43h-13v-4a22.47,22.47,0,0,1-5.43,3.53,13.62,13.62,0,0,1-5.59,1.28,16.52,16.52,0,0,1-5.9-1,15.59,15.59,0,0,1-4.76-2.89,13.56,13.56,0,0,1-3.17-4.28,12.41,12.41,0,0,1-1.15-5.29V37.94Z" class="cls-1"/><g id="g905"><g id="g890"><path id="path13" d="M105.79,36.33a8.9,8.9,0,1,1,8.91-8.9A8.91,8.91,0,0,1,105.79,36.33Z" class="cls-3"/><path id="path25" d="M127.18,36.33a8.9,8.9,0,1,1,8.91-8.9A8.91,8.91,0,0,1,127.18,36.33Z" class="cls-4"/><path id="path31" d="M116.49,17.8a8.9,8.9,0,1,1,8.9-8.9,8.89,8.89,0,0,1-8.9,8.9Z" class="cls-5"/></g><path id="path3823" d="M100.14,40.6l13-3.58V80.94h-13Z" class="cls-1"/></g><path id="path3808" d="M140.14,58.77a37.64,37.64,0,0,0-3.77,1.87,21.89,21.89,0,0,0-3.46,2.3,12.77,12.77,0,0,0-2.55,2.67,5.12,5.12,0,0,0-1,2.94,8.53,8.53,0,0,0,.32,2.34,7,7,0,0,0,.87,1.91,5.15,5.15,0,0,0,1.23,1.27,2.67,2.67,0,0,0,1.51.48,6.3,6.3,0,0,0,3.18-1,41.31,41.31,0,0,0,3.62-2.47Zm13,22.17h-13V77.52c-.71.61-1.42,1.17-2.11,1.67a14.2,14.2,0,0,1-2.3,1.35,13.56,13.56,0,0,1-2.82.88,19.75,19.75,0,0,1-3.78.31,16,16,0,0,1-5.33-.83,12.23,12.23,0,0,1-4-2.31,10.23,10.23,0,0,1-2.51-3.53,11,11,0,0,1-.87-4.37,10.27,10.27,0,0,1,.91-4.42,13.11,13.11,0,0,1,2.55-3.57,19.36,19.36,0,0,1,3.77-2.86,40.26,40.26,0,0,1,4.65-2.31c1.67-.69,3.4-1.32,5.17-1.91l5.25-1.71,1.43-.31V49.34a11.91,11.91,0,0,0-.44-3.45,5.82,5.82,0,0,0-1.15-2.31,4,4,0,0,0-1.79-1.31,6.6,6.6,0,0,0-2.34-.4,7.38,7.38,0,0,0-2.59.4,4.37,4.37,0,0,0-1.67,1.11,3.94,3.94,0,0,0-.91,1.59,6.52,6.52,0,0,0-.28,2,9.51,9.51,0,0,1-.28,2.35,4.85,4.85,0,0,1-.91,2A4.47,4.47,0,0,1,126,52.6a6.84,6.84,0,0,1-2.9.52,7.51,7.51,0,0,1-2.51-.4,6.16,6.16,0,0,1-1.91-1.15,6,6,0,0,1-1.27-1.75,5.59,5.59,0,0,1-.44-2.18,6.42,6.42,0,0,1,1.51-4.1,13.16,13.16,0,0,1,4.06-3.3,23.45,23.45,0,0,1,5.92-2.14,31.07,31.07,0,0,1,7.12-.8,32.21,32.21,0,0,1,7.87.84,16.37,16.37,0,0,1,5.49,2.34,9.55,9.55,0,0,1,3.18,3.66,10.91,10.91,0,0,1,1,4.81Z" class="cls-1"/></g></g></g></g></g></g></g></svg>
\ No newline at end of file
2022/2022-06-08_JuliaForNewcomers/slides/img/r3-training-logo.png

32.4 KiB

2022/2022-06-08_JuliaForNewcomers/slides/img/unicodeplot.png

9.93 KiB

2022/2022-06-08_JuliaForNewcomers/slides/img/whyjulia.png

2.32 MiB

# Julia for newcomers
## June 8th, 2022
<div style="top: 6em; left: 0%; position: absolute;">
<img src="theme/img/lcsb_bg.png">
</div>
<div style="top: 1em; left: 60%; position: absolute;">
<img src="slides/img/r3-training-logo.png" height="200px">
<img src="slides/img/julia.svg" height="200px">
<h1 style="margin-top:3ex; margin-bottom:3ex;">Julia for newcomers</h1>
<h4>
Laurent Heirendt, Ph.D.<br>
Miroslav Kratochvíl, Ph.D.<br><br>
R3 Team - <a href="mailto:lcsb-r3@uni.lu">lcsb-r3@uni.lu</a><br>
<i>Luxembourg Centre for Systems Biomedicine</i>
</h4>
</div>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/SebastianAigner/twemoji-amazing@1.0.0/twemoji-amazing.css">
<style>
code {border: 2pt dotted #f80; padding: .4ex; border-radius: .7ex; color:#444; }
.reveal pre code {border: 0; font-size: 18pt; line-height:27pt;}
em {color: #e02;}
li {margin-bottom: 1ex;}
div.leader {font-size:400%; line-height:120%; font-weight:bold; margin: 1em;}
section {padding-bottom: 10em;}
</style>
# Motivation first!
*Why is it good to work in compiled language?*
- Programs become much faster for free.
*What do we gain by having types in the language?*
- Generic programming, and lots of optimization possibilities for the compiler.
*Is Julia ecosystem ready for my needs? <i class="twa twa-thinking-face"></i>*
- Likely. If not, extending the packages is super easy.
- Base includes most of the functionality of Matlab, R and Python with numpy,
and many useful bits of C++
# Why Julia?
<center><img src="slides/img/whyjulia.png" width="80%"></center>
<div class=leader>
<i class="twa twa-bar-chart"></i>
<i class="twa twa-blue-book"></i>
<i class="twa twa-computer-disk"></i>
<i class="twa twa-chart-increasing"></i><br>
Working with data
</div>
# What do we usually need?
- we need a comfortable abstraction over "long" tabular data → *data frames*
- we need to get the data in and out → *IO functions*
- we need to make pictures → *plotting packages*
# Writing files
All at once:
```julia
write("file.txt", "What a string!\n")
write("data.bin", UInt32[1,2,3])
```
By parts (with streaming, etc.):
```julia
f = open("file.txt", "w")
println(f, "This is my string!")
# ...
close(f)
```
Better:
```julia
open("file.txt", "a") do f
println(f, "The string again!")
# ...
end
```
# Reading files
All at once:
```julia
read("file.txt", String)
open(x -> collect(readeach(x, UInt32)), "data.bin", "r")
```
Process all lines:
```julia
for line in eachline("file.txt")
println("got a line: " * line)
end
```
Manually:
```julia
open("inputs.txt", "r") do io
a = parse(Int, readline(io))
b = parse(Int, readline(io))
println("$a * $b = $(a*b)")
end
```
# Extremely useful: string interpolation
Pasting strings manually is _boring_.
```julia
"String contains $val and $otherval."
do_something("input$i.txt", "output$i.txt")
"$str <<-->> $(reverse(str))"
```
The conversion to actual strings is done using `show()`. (Customize by overloading!)
# Reading and writing structured data
```julia
using DelimitedFiles
mtx = readdlm("matrix.tsv", '\t', Int, '\n') # returns a Matrix{Int}
writedlm("matrix.csv", ',')
```
# Data frames
Package `DataFrames.jl` provides a work-alike of the data frames from
other environments (pandas, `data.frame`, tibbles, ...)
```julia
using DataFrames
mydata = DataFrame(id = [32,10,5], text = ["foo", "bar", "baz"])
mydata.text
mydata.text[mydata.id .>= 10]
```
Main change from `Matrix`: *columns are labeled and their types differ*, also entries may be missing
# DataFrames
Popular way of importing data:
```julia
using CSV
df = CSV.read("database.csv", DataFrame) # can also do a Matrix
CSV.write("backup.csv", df)
```
Popular among computer users:
```julia
using XLSX
x = XLSX.readxlsx("important_results.xls")
XLSX.sheetnames(x)
DataFrame(XLSX.gettable(x["Results sheet"])...)
```
<small>(Please do not export data to XLSX.)</small>
# Plotting
<center>
<img src="slides/img/unicodeplot.png" width="40%" />
</center>
# Usual plotting packages
- `UnicodePlots.jl` (useful in terminal, https://github.com/JuliaPlots/UnicodePlots.jl)
- `Plots.jl` (matplotlib workalike, works with Plotly)
- `GLMakie.jl` (interactive plots)
- `CairoMakie.jl` (PDF export of Makie plots)
Native `ggplot` and `cowplot` ports are in development.
Gallery available: https://makie.juliaplots.org
<div class=leader>
<i class="twa twa-blue-circle"></i>
<i class="twa twa-red-circle"></i>
<i class="twa twa-green-circle"></i>
<i class="twa twa-purple-circle"></i><br>
Julia language primer
</div>
# Expressions and types
Expressions and types You can discover types of stuff using `typeof`.
Common types:
- `Bool`
```julia
false, true
```
- `Char`
```julia
'a', 'b', ...
```
- `String`
```julia
"some random text"
```
- `Int`
```julia
1, 0, -1, ...
```
- `Float64`
```julia
1.1, 0, -1, ...
```
# Types may have parameters (usually "contained type")
- `Vector{Int}`
```julia
[1, 2, 5, 10]
```
- `Matrix{Float64}`
```julia
[1.0 2.0; 2.0 1.0]
```
- `Tuple`
```julia
(1, 2.0, "SomeLabel")
```
- `Set{Int}`
- `Dict{Int,String}`
(default parameter value is typically `Any`)
# Basic functionality and expectable stuff
- Math: `+`, `-`, `*`, `/`, `^`, ...
- Logic: `==`, `!=`, `<`, `>`, `<=`, `>=`, `&&`, `||`, `!`, ...
- Assignment: `=`, `+=`, `-=`, `*=`, ...
- I/O: `open`, `println`, `read`, `readlines`, ...
- Arrays: `array[1]`, `array[2:5]`, `array[begin+1:end-1]`, `size`, `length`, `cat`, `vcat`, `hcat`, ...
Most functions are *overloaded* to *efficiently* work with multiple types of data.
Functionality is easy to discover by just `Tab`bing the definitions, also `methods(...)` and `methodswith(...)`.
# Control flow: Commands and code blocks
Typically you write 1 command per 1 line.
Commands can be separated by semicolons, and grouped using code blocks:
```julia
begin
a = 10
b = 20; b += 20
a + b # implicit return!
end
```
Many constructions (cycles, function definitions) start the block
automatically, you only write `end`.
# Control flow: Conditional execution
- Traditional `if`:
```julia
if condition
actions
else # optional
actions # optional
end
```
- Shorter inline condition:
```julia
myfunction( index<=10 ? array[index] : default_value )
```
- <i class="twa twa-light-bulb"></i> Useful shell-like shortcuts:
```julia
a < 0 && (a = 0)
a > 10 && (a = 10)
isfinite(a) || @error "a is infinite, program will crash!"
```
# Control flow: Doing stuff many times
Iteration count-based loop:
```julia
for var = iterable # , var2 = iterable2, ...
code(variable, variable2)
# ...
end
```
Syntax with `in` instead of `=` is also supported.
Examples:
```julia
for i = 1:10
@info "iterating!" i
end
for i = 1:10, j = 1:10
matrix[i,j] = i*j
end
```
Utilities: `eachindex`, `enumerate`
# Control flow: Doing stuff many times
Condition satisfaction-based loop:
```julia
while condition
do_something() # condition is true
end
# condition is false
```
Example:
```julia
number = 123519
digit_sum = 0
while number > 0
digit_sum += number % 10
number ÷= 10
end
@info "We've got results!" digit_sum
```
<div class=leader>
<i class="twa twa-hot-beverage"></i><i class="twa twa-bubble-tea"></i><i class="twa twa-beverage-box"></i><br>
Short break
</div>
Let's have *10 minutes* for a coffee or something.
(Questions?)
<div class=leader>
<i class="twa twa-blue-circle"></i>
<i class="twa twa-red-circle"></i>
<i class="twa twa-green-circle"></i>
<i class="twa twa-purple-circle"></i><br>
Julia language primer<br>(part 2)
</div>
# <i class="twa twa-light-bulb"></i> Structured cycles!
Using functional-style loops is *much less error-prone* to indexing
errors.
- Transform an array:
```julia
map(sqrt, [1,2,3,4,5])
map((x,y) -> (x^2 - exp(y)), [1,2,3], [-1,0,1])
```
- Summarize an array:
```julia
reduce(+, [1,2,3,4,5])
reduce((a,b) -> "$b $a", ["Use", "the Force", "Luke"])
reduce(*, [1 2 3; 4 5 6], dims=1)
```
**Tricky question (<i class="twa twa-light-bulb"></i><i class="twa twa-light-bulb"></i><i class="twa twa-light-bulb"></i>):** What is the overhead of the "nice" loops?
# Making new arrays with loops
```julia
julia> [i*10 + j for i = 1:3, j = 1:5]
3×5 Matrix{Int64}:
11 12 13 14 15
21 22 23 24 25
31 32 33 34 35
julia> join(sort([c for word in ["the result is 123", "what's happening?", "stuff"]
for c in word
if isletter(c)]))
"aaeeeffghhhiilnnpprssssttttuuw"
```
# Control flow: subroutines (functions)
- Multi-line function definition
```julia
function combine(a,b)
return a + b
end
```
- "Mathematical" neater definition
```julia
combine(a,b) = a + b
```
- <i class="twa twa-light-bulb"></i> Definition with types specified (prevents errors, allows optimizations!)
```julia
function combine(a::Int, b::Int)::Int
return a + b
end
```
# <i class="twa twa-light-bulb"></i><i class="twa twa-light-bulb"></i> Control flow: subroutine overloading (methods)
- A method for combining integers
```julia
combine(a::Int, b::Int)::Int = a + b
```
- A method of the "same function" for combining strings
```julia
combine(a::String, b::String)::String = "$a and $b"
```
# <i class="twa twa-light-bulb"></i><i class="twa twa-light-bulb"></i><i class="twa twa-light-bulb"></i> Supertype hierarchy
Types possess a single supertype, which allows you to easily group
multiple types under e.g. `Real`, `Function`, `Type`, `Any`, ...
This creates *groups of types* that are useful for restricting your functions to work on the most reasonable subsets of inputs.
<pre style="font-size: 80%; line-height:120%;"><code class="language-julia hljs">
julia> Int
Int64
julia> Int.super
Signed
julia> Int.super.super
Integer
julia> Int.super.super.super
Real
julia> Int.super.super.super.super
Number
julia> Int.super.super.super.super.super
Any
</code></pre>
(Upon calling the function, Julia picks the *most specific* available method.)
# Function arguments
- Keyword arguments (can not be used for overloading)
```julia
function f(a, b=0; extra=0)
return a + b + extra
end
f(123, extra=321)
```
- <i class="twa twa-light-bulb"></i> Managing arguments en masse
```julia
euclidean(x; kwargs...) = sqrt.(sum(x.^2; kwargs...))
max_squared(args...) = maximum(args .^ 2)
```
# Broadcasting over iterable things
- Broadcasting operators by prepending a dot
```julia
matrix[row, :] .+= vector1 .* vector2
```
- Broadcasting a function
```julia
sqrt.(1:10)
maximum.(eachcol(rand(100,100)))
x = [1,2,3,4]
x' .* x
```
<i class="twa twa-light-bulb"></i> The "magic dot" is a shortcut for calling `broadcast(...)`.
# Advanced container types
- Dictionaries (`Dict{KeyType, ValueType`) allow O(log n) indexing, great for
lookups or keyed data structures. Contents may be typed for increased
efficiency.
```julia
person = Dict("name" => "John", "surname" => "Foo", "age" => 30)
person["age"]
indexof(v::Vector) = Dict(v .=> eachindex(v))
```
- <i class="twa twa-light-bulb"></i> Sets are key-only containers (keys are _unique_)
```julia
julia> x=Set([1,2,3,2,1]);
julia> println(x)
Set([2, 3, 1])
julia> push!(x,5);
julia> push!(x,5);
julia> println(x)
Set([5, 2, 3, 1])
```
[
{ "filename": "index.md" },
{ "filename": "overview.md" },
{ "filename": "intro.md" },
{ "filename": "bootstrap.md" },
{ "filename": "language.md" },
{ "filename": "io.md" },
{ "filename": "distributed.md" },
{ "filename": "thanks.md" }
]
# Overview
1. Why would you learn another programming language again?
2. Bootstrapping, working with packages, writing a script (30m)
3. Language and syntax primer (35m) with a pause (10m) in the middle
4. Getting the data in and out (15m)
5. Running programs on ULHPC (15m)
6. Questions, hands-on, time buffer (15m)
# Thank you!
<center><img src="slides/img/r3-training-logo.png" height="200px"></center>
Contact us if you need help:
<a href="mailto:lcsb-r3@uni.lu">lcsb-r3@uni.lu</a>