Distill
Kasun Vithanage

The world is scary
Anything can go wrong

Yes, anything
- Your network can go down
- Harddisk can fail
- Packets might be lost
- There can be unhandled errors
- etc...
Some Systems cannot go down
That easily
- Telephone Systems
- Network Systems
- Nuclear Weapon Systems
- Healthcare Systems
- Banking Systems
- etc...
A Problem In Telecom

- Telecom Systems need to be reliable and highly available(non-stop)
- At that time(in the 80s) telephone switches were written using C
- Upgrading the software of these switches was hard
- Specially when people still calling someone all the time
- People at Ericcson wanted to solve this problem
was born 🎂
- Erlang was created by Joe Armstrong, Robert Virding and Mike Williams in 1980
- The first implementation was in Prolog
- Later it was implemented in C
- Syntax was similar to Prolog 😪
- The virtual machine that runs Erlang Code is known as BEAM VM

Joe Armstrong
Traits of Erlang
- A Functional Language
- Distributed
- Concurrent
- Fault Tolerant
- Low-Latency
- Robust
This makes Erlang suitable for
Soft-Realtime Systems 😚
- Immutability
- Process Isolation
- Message Passing
- Pattern Matching
It also has
Erlang in Action

Factorial
In Mathematics
In Erlang
% module definition
-module(fact).
-export([fac/1]).
fac(0) -> 1;
fac(N) when N > 0, is_integer(N) -> N * fac(N-1)."Small" Companies Using Erlang
- Cisco
- Goldman Sachs
- Heroku
- Nintendo
- Riot Games(For LoL Chat)
- Klarna.
More from here






José was a RoR(Ruby on Rails) contributor.
He wanted to make it safe and performant 🚀
Instead, he ended up creating a new programming language 😬
So dont be like him, please 😰
No, I'm kidding...

José Valim
Elixir has a modern clean look, its like reading English 😎
Finally, you can use all cores of your VPS or whatever to run your dream app which scales to millions of users 😍
Of course, it's functional...but you don't need to learn Monads, Functors or do a PhD to understand code 🤓
Fortunately, it compiles to Erlang BEAM bytecodes, so you can access the Erlang ecosystem without any problem 🎉
Only "small" Companies uses Elixir 😏
- Discord
- Heroku
- Change.org
- PepsiCo
- XPlane
- Slab
- Adobe

More from here





Ok Let's make a potion

Basic Data Types
-
Integers
- 123
- 0b100
- 0xFA
- 0o56
-
Floats
- 64-bit double precision
- 0.112
- 1.0e-10
-
Atoms(or Symbols)
- Represents distinct values
-
:ok,:error -
true, false, nilare special atoms
-
Strings
- Strings are double quoted
- "hello world"
- UTF-8 Supported
- Single quoted values represent charlists
-
Booleans
trueorfalse1 == 1istrue1 == 3isfalse
-
Tuples
-
{:ok, "1", 1.2}is a tuple - They can hold any value
- Contiguously stored in memory
- Often used to return multiple values
-
-
Lists
- [1, 2, "3", 4.4] is a list
- Stored as a Linked List in memory
- Prepending to list is fast, Appending is slow

Where are Functions?
Yes, Elixir has first-class support for functions
Anonymous functions
- Starts with fn and ends with end
- To invoke, you need to use a dot(.)
-
add.(1, 2)would give3
-
- Parenthesis are optional
- Number of arguments for functions known as arity of the function
-
add/2means add has 2 args
-
- We will discuss about named functions
add = fn a, b -> a + b end
IO.puts(add.(1, 2))
Some Basic Operators
Basic Arithmetic
Operators like +, -, / and * are used with numerical types.
div, rem can be used with integer division
List Operators
++ and -- can be used to manipulate lists
iex(1)> [1, 2, 3] ++ [4, 5]
[1, 2, 3, 4, 5]
iex(2)> [1, 2, 3] -- [1]
[2, 3]
<> is used to concat strings
String Operators
iex(1)> "hello" <> "world"
"helloworld"
Binary Operators
Operators like and, or, not used with boolean values only
&&, ||, ! will work with any value. Everything except nil or false will be evaluated to truthy
Pattern Matching

= in Elixir is called Match Operator.
It can match left side and right side.
iex> x = 1
1
iex> 1 = x
1
iex> 1 = y # since y is not defined, we cant match this.
** (CompileError) iex:11: undefined function y/0 (there is no such import)
When you write x = 1, elixir will bind value 1 to variable x.
You can also rebind x to another value later as well.
iex> x = 1
1
iex> x = "wave"
"wave"
iex> x = "zync"
"zync"
iex> x
"zync"Match operator works on complex values as well
iex> {a, b, c} = {:hello, "world", 123}
{:hello, "world", 123}
iex> a
:hello
iex> b
"world"
iex> c
123
With Tuples
iex> list = [1, 2, 3, 4, 5]
[1, 2, 3, 4, 5]
iex> [head | tail] = list
[1, 2, 3, 4, 5]
iex> head
1
iex> tail
[2, 3, 4, 5]
With Lists
If you want to match against an existing value without rebinding ^ (pin operator) is your friend
iex> x = 1
1
iex> ^x = 2
** (MatchError) no match of right hand side value: 2
iex> {a, ^x, b} = {1, 3, 1}
** (MatchError) no match of right hand side value: {1, 3, 1}
iex> {a, ^x, b} = {1, 1, 1}
{1, 1, 1}
You can do cool stuffs with Elixir

More Data Structures 😎
Keyword Lists
Keyword List is a special list of two element tuples
iex> [{:hello, "world"}, {:age, 25}]
[hello: "world", age: 25]
iex> [hello: "world", age: 25]
[hello: "world", age: 25]There are 3 special charasteristics
- Keys must be atoms
- Keys are ordered, as specified by the developer
- Keys can be given more than once
This is really useful when passing options to functions
Maps
Maps are the "Go To" data structure in Elixir
Maps can have any value as keys
Maps also don't follow any ordering
iex> my_map = %{:hello => "world", "hi" => "elixir", 1 => 2}
%{1 => 2, :hello => "world", "hi" => "elixir"}
iex> my_map[:hello]
"world"
iex> my_map["hi"]
"elixir"
iex> my_map[1]
2Control Structures

if and unless
Elixir doesn't have those built into the language, instead they are macros
iex> if String.length("hello") == 5 do
...> "its five"
...> else
...> "oh no"
...> end
"its five"
unless is the opposite of if
iex> unless String.length("hello") == 5 do
...> "oh no"
...> else
...> "its five"
...> end
"its five"Note that there is no return keyword.
case
if/unless works with boolean or nil only
case brings power of pattern matching into the play
iex> case {1, 2, 3} do
...> {1, x, 3} -> "matches x and binds it to 2"
...> _ -> "nothing matches any clause"
...> end
"matches x and binds it to 2"We can be more selective with guards
iex> case {1, 2, 3} do
...> {1, x, 3} when x > 0 -> "matches x and binds it to 2"
...> _ -> "nothing matches any clause"
...> end
"matches x and binds it to 2"You can apply multiple guards to same clause
Not only in case, multiple clauses can be used with functions too
iex> add = fn
...> x, y when is_number(x) and is_number(y) -> x + y
...> x, y when is_binary(x) and is_binary(y) -> x <> y
...> x, y -> "hmm not sure what to do"
...> end
#Function<13.126501267/2 in :erl_eval.expr/5>
iex> add.(100, 1)
101
iex> add.("hello", "elixir")
"helloelixir"
iex> add.("hi", 1)
"hmm not sure what to do"Note that is_number and is_binary are guards
You can also write custom guards 💂♂️
With anonymous functions, you need to maintain same aritey 🤓
cond
This is similar to if/else if in other langs.
iex> cond do
...> 1 == 1 -> "its true, i knew it"
...> :a == false -> "who is the imposter"
...> end
"its true, i knew it"This is not widely used with elixir
But its there just in case 😏

Did you notice something?

Every statement returns a value.
Yes, even if, cond, case too
So we can write code like this 😎
iex> result = case {1, 2, 3} do
...> {1, x, 3} -> x * 10
...> {x, x, x} -> x * 3 # it matches when all elems are same
...> _ -> 100
...> end
20Distill Elixir
By Kasun Vithanage
Distill Elixir
Let's Distill some Elixir...Beginners guide to distilling elixir and functional programming
- 799