# Julia at a Glance — A Language a Day, Advent Calendar 2019 Day 3/24

Welcome to Day 3 of this year’s A Language a Day Advent Calendar. Today’s topic is introduction to the Julia programming language.

• A dynamically typed language
• Compiles to LLVM
• Strong support of scientific and numeric computing (math, statistics, etc.)
• Appeared in 2012
• Website: julialang.org

## Installing and running Julia

To install Julia, visit its official website and choose a package for your operating system. You may need to update your PATH variable to point it to the installation directory:

export PATH=$PATH:/Applications/Julia-1.3.app/Contents/Resources/julia/bin/ To run a program, pass its name to the interpreter: $ julia hello-world.jl

## Hello, World!

The Hello, World! program in Julia is very straightforward:

println("Hello, World!")

## Variables

In Julia, you create variables when you assign a value to it:

answer = 42

### Identifiers

An interesting feature of Julia is that you can use Unicode letters in identifiers, and, what is extremely exciting, its REPL shell allows you typing such symbols using TeX sequences. For example, to get δ you can type \delta and press the Tab key.

Variables can be interpolated in strings using the $ prefix. In the following snippet, the parentheses are added to not to treat the exclamation sign as part of the variable name. name = "Julien" println("Bonjour,$name !")

### A Polymorphic example

Let us create a program that uses an array of dogs and cats, each being an animal, and then it loops over them to print a message depending on the actual type of a data item in hand.

We will define an abstract type Animal, and derive two other types: structures Dog and Cat. Inheritance is indicated by the <: symbol. For simplicity, these types do not contain any data fields, but you may extend the example to include pet names, for example.

abstract type Animal end
struct Dog <: Animal end
struct Cat <: Animal end

speak(::Cat) = println("Cat")
speak(::Dog) = println("Dog")

zoo = [Cat(); Dog(); Cat()]
for x in zoo
speak(x)
end

As it is not possible to have what you would call class methods in other languages, you can use multiple dispatch and define the two functions speak that behave differently depending on the type of the argument. If it is a Cat, it prints Cat, and if the type is Dog, it prints Dog.

## Concurrency and parallelism

In Julia, they have tasks and channels (single-threaded), multi-threading (which is experimental so far), and real parallelism — both multi-core and distributed.

### Channels

One of the methods to communicate between different parts of a concurrent program are channels. Let us try them out on a single-threaded program that first fills a channel with some numbers and then reads them.

ch = Channel(10)

range = 1:5
foreach(n -> put!(ch, 2n), range)

for _ in range
println(take!(ch))
end

When you create a channel, you specify how spacious it can be: in the given example, it can keep a queue of 10 elements. You send data to the channel using the put! function (it obviously modifies it, thus an exclamation mark). Reading is done with take!, which waits until there is enough data in the channel.

### Sleep Sort

Below, the two implementations of the algorithms are given.

In both of them, a separate task is scheduled for each number using the @async macro.

In the first variant, the current number is printed directly from the task, and to wait for them to complete, the whole loop is wrapped by the @sync macro.

data = [10, 4, 2, 6, 2, 7, 1, 3]
function sort_value(n)
sleep(n / 10)
println(n)
end

@sync for n in data
@async sort_value(n)
end

In the second implementation, channels are used to transfer the result. In this case, you do not need to sync the tasks, as reading from the channel will block the program until all items are passed through.

data = [10, 4, 2, 6, 2, 7, 1, 3]
data_size = size(data)[1]
ch = Channel(data_size)

function sort_value(n)
sleep(n / 10)
put!(ch, n)
end

for n in data
@async sort_value(n)
end

for _ in 1:data_size
n = take!(ch)
println(n)
end

## Get more

To enjoy the language even more, I suggest you continue by taking a look at the following resources:

The source code of today’s examples are pushed to GitHub.

Next: Day 4. Kotlin