SlideShare a Scribd company logo
Erlang & Elixir
workshop #1 :: erlang 101
Created by /Krzysztof Marciniak @hun7err
Erlang
You can the introduction.skip
Source:
What is Erlang?
Erlang is a programming language used to build
massively scalable soft real-time systems with
requirements on high availability. Some of its uses
are in telecoms, banking, e-commerce, computer
telephony and instant messaging. Erlang's runtime
system has built-in support for concurrency,
distribution and fault tolerance.
Erlang homepage
Source:
What is Erlang?
Erlang's syntax is very similar to Prolog's, but the
semantics are very different. An early version of
Erlang was written using Prolog, but today's Erlang
can no longer meaningfully be said to be "based
on Prolog."
StackOverflow
Why Erlang?
it's functional!
it's multi-threaded!
high-availability
easy distribution
code hot-swap
Components of Erlang
virtual machine - the new BEAM (Bogdan/Björn's Erlang Abstract
Machine)
OTP - Open Telecom Platform, a framework/library
erl - Erlang interactive console
rebar* - an Erlang build tool [ ]GitHub
* technically it's not an official component, but it is very useful
The absolute basics
-module(ex1).
-export([add/2]).
add(X, Y) ->
X + Y.
1> c(ex1).
{ok,ex1}
2> ex1:add(2, 3).
5
3>
$ erl -man [module_name]
io, lists, etc.
Recursive functions
-module(fac).
-export([fac/1]).
fac(1) ->
1; % function clause
fac(N) ->
N * fac(N - 1).
Higher-order functions
1> X = fun(X) -> X * 4 end.
2> X(4).
16
3>
Lists
[Head | Tail] = [1,2,3,4] % list decomposition
[First, Second | Tail] = [1,2,3,4] % ( ͡° ͜ʖ ͡°)
lists:reverse([1,2,3,4])
lists:append([1,2], [3,4])
lists:append([[1,2],[3,4],[5,6]])
lists:filter(fun(X) -> X rem 2 == 0 end, [1,2,3,4,5,6]) % only even numbers
lists:foldl(fun(X, Sum) -> X + Sum end, 0, [1,2,3,4,5]) % sum
% ^--- fold left, might sound familiar
% erl -man lists
Stop! Hammer time.
(excercises)
1. Create a list_reverse function
2. Write a custom map function (list_map)
Lightweight threads
-module(simplethreads).
-export([start/1]).
hello(Text) ->
io:format("Hello, ~p!~n", [Text]).
start() ->
spawn(simplethreads, hello, ["World"]).
That should return PID of the spawned process, i.e. <0.59.0>
Threads + Recursion =
Awesomeness
-module(threads).
-export([start/0, say/2]).
say(_, 0) ->
done;
say(Text, Times) ->
io:format("~p~n", [Text]),
say(Text, Times-1).
start() ->
spawn(threads, say, ["hello", 5]),
spawn(threads, say, ["bye", 5]).
Interprocess communication
(1/2)
-module(simplethreads).
-export([start/0, say/0]).
say() ->
receive
Text ->
io:format("Hello, ~p!~n", [Text])
end.
start() ->
Pid = spawn(simplethreads, say, []),
timer:sleep(200),
Pid ! "World",
ok.
Interprocess communication
(2/2.1)
-module(pingpong).
-export([start/0]).
ping(0, PongPID) ->
PongPID ! finished,
io:format("ping finished~n", []);
ping(N, PongPID) ->
PongPID ! {ping, self()},
receive
pong ->
io:format("pong~n", [])
end,
ping(N-1, PongPID).
% continued on the next slide
Interprocess communication
(2/2.2)
pong() ->
receive
finished ->
io:format("pong finished~n", []);
{ping, PingPID} ->
io:format("ping~n", []),
PingPID ! pong,
pong()
end.
start() ->
PongPID = spawn(pingpong, pong, []),
spawn(pingpong, ping, [5, PongPID]).
Interprocess communication
Eshell V7.1 (abort with ^G)
1> c(pingpong).
{ok,pingpong}
2> pingpong:start().
ping
<0.42.0>
pong
ping
pong
ping
pong
ping
pong
ping
pong
ping finished
pong finished
3>
OTP - Open Telecom Platform
OTP stands for Open Telecom Platform, although
it's not that much about telecom anymore (it's
more about software that has the property of
telecom applications, but yeah.) If half of Erlang's
greatness comes from its concurrency and
distribution and the other half comes from its
error handling capabilities, then the OTP
framework is the third half of it.
OTP example
-module(server).
-behaviour(myserver).
-export([ % The behaviour callbacks
init/1, % - initializes our process
handle_call/3, % - handles synchronous calls (with response)
handle_cast/2, % - handles asynchronous calls (no response)
handle_info/2, % - handles out of band messages (sent with !)
terminate/2, % - is called on shut-down
code_change/3]). % - called to handle code changes
Elixir
The new youth of Erlang
What is Elixir?
Elixir is a dynamic, functional language designed
for building scalable and maintainable
applications. Elixir leverages the Erlang VM, known
for running low-latency, distributed and fault-
tolerant systems, while also being successfully
used in web development and the embedded
software domain.
The basics
:hello # an atom
"utf string ąę" # in erlang that would not be so easy
hello = "a thing" # no longer capitalized, yay!
hello = :hello # notice how we can overwrite the value
IO.puts("hellonworld")
length([1,2,3]) # I'll speak of lists in a second
length [1,2,3] # notice how we can skip brackets
Lists
iex> a_list = [1,2,3,4,5]
[1,2,3,4,5]
iex> a_list = a_list ++ [6]
[1,2,3,4,5,6]
iex> a_list -- [1,3,5]
[2,4,6]
iex> [head|tail] = [1,2,3,4] # also hd/1, tl/1
[1,2,3,4]
iex> head
1
iex> tail
[2,3,4]
iex> [104, 101, 108, 108, 111]
"hello"
Tuples
iex> tuple = {:ok, "hello"}
{:ok, "hello"}
iex> put_elem(tuple, 1, "world")
{:ok, "world"}
iex> tuple # Elixir types are immutable
{:ok, "hello"}
iex> tuple_size tuple
2
Modules and functions
defmodule Calculator do
def add(x, y) do
x + y
end
end
defmodule Lists do
def reverse([head|tail], acc) do
reverse(tail, [head|acc])
end
# function clauses do not have to be distinguished
def reverse([], acc) do
acc
end
end
If macro, keywords, maps and
dicts
iex> if false, do: :this, else: :that
:that
iex> if(false, [do: :this, else: :that])
:that
iex> if(false, [{:do, :this}, {:else, :that}])
:that
iex> map = %{:a => 1, :b => 2}
%{a: 1, b: 2}
iex> list = [a: 1, b: 3]
[a: 1, b: 3]
iex> Dict.put list, :a, 4
[a: 4, b: 3]
iex> Dict.put map, :a, 4
%{a: 4, b: 2}
Phoenix framework
Phoenix is a web development framework written
in Elixir which implements the server-side MVC
pattern. Many of its components and concepts will
seem familiar to those of us with experience in
other web frameworks like Ruby on Rails or
Python's Django.
Getting started with Phoenix
$ mix local.hex
$ mix archive.install https://github.com/phoenixframework/phoenix/releases/download/v1.1.0/phoen
$ mix phoenix.new hello
$ mix ecto.create
$ mix phoenix.server
Bibliography
The official "Getting started" guide -
Learn You Some Erlang -
Erlang Doc on distributed systems -
OTP for beginners -
Elixir Lang homepage -
Phoenix Framework homepage -
http://www.erlang.org/download/getting_started-5.4.pdf
http://learnyousomeerlang.com/
link
link
http://elixir-lang.org/
http://www.phoenixframework.org/

More Related Content

PDF
Introduction to Erlang/(Elixir) at a Webilea Hands-On Session
PDF
Elixir Into Production
PDF
Introduction to Elixir
PDF
VerneMQ @ Paris Erlang User Group June 29th 2015
PDF
ElixirConf Lightning Talk: Elixir |> Production
PDF
Hello elixir (and otp)
PDF
Learning Elixir as a Rubyist
PDF
Yurii Bodarev - OTP, Phoenix & Ecto: Three Pillars of Elixir
Introduction to Erlang/(Elixir) at a Webilea Hands-On Session
Elixir Into Production
Introduction to Elixir
VerneMQ @ Paris Erlang User Group June 29th 2015
ElixirConf Lightning Talk: Elixir |> Production
Hello elixir (and otp)
Learning Elixir as a Rubyist
Yurii Bodarev - OTP, Phoenix & Ecto: Three Pillars of Elixir

What's hot (20)

PPTX
Introduction to Phoenix Framework (Elixir) 2016-01-07
PDF
Yaroslav Martsynyuk - Deploying Elixir/Phoenix with Distillery
PDF
Atmosphere 2014
PDF
8 Minutes On Rack
PDF
Rack Middleware
PDF
Phoenix for Rails Devs
PDF
Phoenix Framework
PDF
Pycon - Python for ethical hackers
PDF
maXbox Starter 42 Multiprocessing Programming
PDF
Cloud meets Fog & Puppet A Story of Version Controlled Infrastructure
KEY
Ruby Concurrency and EventMachine
PDF
Speech for Windows Phone 8
PDF
Découvrir dtrace en ligne de commande.
PDF
Fluentd v0.12 master guide
PDF
Python, do you even async?
PDF
Flask With Server-Sent Event
PDF
Doing It Wrong with Puppet -
PDF
PPTX
Async programming and python
PDF
Exploring Async PHP (SF Live Berlin 2019)
Introduction to Phoenix Framework (Elixir) 2016-01-07
Yaroslav Martsynyuk - Deploying Elixir/Phoenix with Distillery
Atmosphere 2014
8 Minutes On Rack
Rack Middleware
Phoenix for Rails Devs
Phoenix Framework
Pycon - Python for ethical hackers
maXbox Starter 42 Multiprocessing Programming
Cloud meets Fog & Puppet A Story of Version Controlled Infrastructure
Ruby Concurrency and EventMachine
Speech for Windows Phone 8
Découvrir dtrace en ligne de commande.
Fluentd v0.12 master guide
Python, do you even async?
Flask With Server-Sent Event
Doing It Wrong with Puppet -
Async programming and python
Exploring Async PHP (SF Live Berlin 2019)
Ad

Similar to Erlang and Elixir (20)

PDF
Erlang Message Passing Concurrency, For The Win
PDF
Erlang, an overview
PDF
Elixir and OTP Apps introduction
PDF
Origins of Elixir programming language
PPTX
PDF
FunctionalConf '16 Robert Virding Erlang Ecosystem
PDF
Introduction To Erlang Final
PPTX
Elixir introduction
PDF
Introducing Elixir and OTP at the Erlang BASH
PDF
Erlang is not a city in Germany
PDF
Getting started erlang
PPT
Erlang OTP
PDF
Elixir talk
PPT
The Erlang Programming Language
PPTX
Repeating History...On Purpose...with Elixir
PDF
Erlang from behing the trenches by Francesco Cesarini
KEY
Erlang bootstrap course
PDF
Erlang - Concurrent Language for Concurrent World
PPTX
Erlang kickstart
PDF
ElixirConf 2017 - Writing an Editor in Elixir - Ian Duggan
Erlang Message Passing Concurrency, For The Win
Erlang, an overview
Elixir and OTP Apps introduction
Origins of Elixir programming language
FunctionalConf '16 Robert Virding Erlang Ecosystem
Introduction To Erlang Final
Elixir introduction
Introducing Elixir and OTP at the Erlang BASH
Erlang is not a city in Germany
Getting started erlang
Erlang OTP
Elixir talk
The Erlang Programming Language
Repeating History...On Purpose...with Elixir
Erlang from behing the trenches by Francesco Cesarini
Erlang bootstrap course
Erlang - Concurrent Language for Concurrent World
Erlang kickstart
ElixirConf 2017 - Writing an Editor in Elixir - Ian Duggan
Ad

Recently uploaded (20)

PPT
“AI and Expert System Decision Support & Business Intelligence Systems”
PPTX
MYSQL Presentation for SQL database connectivity
PDF
Dropbox Q2 2025 Financial Results & Investor Presentation
PDF
Machine learning based COVID-19 study performance prediction
PDF
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
PPTX
sap open course for s4hana steps from ECC to s4
PDF
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
PDF
Advanced methodologies resolving dimensionality complications for autism neur...
PPTX
Programs and apps: productivity, graphics, security and other tools
PDF
Chapter 3 Spatial Domain Image Processing.pdf
PDF
Approach and Philosophy of On baking technology
PDF
Per capita expenditure prediction using model stacking based on satellite ima...
PPTX
Big Data Technologies - Introduction.pptx
PDF
cuic standard and advanced reporting.pdf
PDF
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
DOCX
The AUB Centre for AI in Media Proposal.docx
PPTX
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
PPTX
Digital-Transformation-Roadmap-for-Companies.pptx
PDF
Encapsulation_ Review paper, used for researhc scholars
PPTX
Understanding_Digital_Forensics_Presentation.pptx
“AI and Expert System Decision Support & Business Intelligence Systems”
MYSQL Presentation for SQL database connectivity
Dropbox Q2 2025 Financial Results & Investor Presentation
Machine learning based COVID-19 study performance prediction
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
sap open course for s4hana steps from ECC to s4
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
Advanced methodologies resolving dimensionality complications for autism neur...
Programs and apps: productivity, graphics, security and other tools
Chapter 3 Spatial Domain Image Processing.pdf
Approach and Philosophy of On baking technology
Per capita expenditure prediction using model stacking based on satellite ima...
Big Data Technologies - Introduction.pptx
cuic standard and advanced reporting.pdf
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
The AUB Centre for AI in Media Proposal.docx
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
Digital-Transformation-Roadmap-for-Companies.pptx
Encapsulation_ Review paper, used for researhc scholars
Understanding_Digital_Forensics_Presentation.pptx

Erlang and Elixir

  • 1. Erlang & Elixir workshop #1 :: erlang 101 Created by /Krzysztof Marciniak @hun7err
  • 2. Erlang You can the introduction.skip
  • 3. Source: What is Erlang? Erlang is a programming language used to build massively scalable soft real-time systems with requirements on high availability. Some of its uses are in telecoms, banking, e-commerce, computer telephony and instant messaging. Erlang's runtime system has built-in support for concurrency, distribution and fault tolerance. Erlang homepage
  • 4. Source: What is Erlang? Erlang's syntax is very similar to Prolog's, but the semantics are very different. An early version of Erlang was written using Prolog, but today's Erlang can no longer meaningfully be said to be "based on Prolog." StackOverflow
  • 5. Why Erlang? it's functional! it's multi-threaded! high-availability easy distribution code hot-swap
  • 6. Components of Erlang virtual machine - the new BEAM (Bogdan/Björn's Erlang Abstract Machine) OTP - Open Telecom Platform, a framework/library erl - Erlang interactive console rebar* - an Erlang build tool [ ]GitHub * technically it's not an official component, but it is very useful
  • 7. The absolute basics -module(ex1). -export([add/2]). add(X, Y) -> X + Y. 1> c(ex1). {ok,ex1} 2> ex1:add(2, 3). 5 3> $ erl -man [module_name] io, lists, etc.
  • 8. Recursive functions -module(fac). -export([fac/1]). fac(1) -> 1; % function clause fac(N) -> N * fac(N - 1).
  • 9. Higher-order functions 1> X = fun(X) -> X * 4 end. 2> X(4). 16 3>
  • 10. Lists [Head | Tail] = [1,2,3,4] % list decomposition [First, Second | Tail] = [1,2,3,4] % ( ͡° ͜ʖ ͡°) lists:reverse([1,2,3,4]) lists:append([1,2], [3,4]) lists:append([[1,2],[3,4],[5,6]]) lists:filter(fun(X) -> X rem 2 == 0 end, [1,2,3,4,5,6]) % only even numbers lists:foldl(fun(X, Sum) -> X + Sum end, 0, [1,2,3,4,5]) % sum % ^--- fold left, might sound familiar % erl -man lists
  • 11. Stop! Hammer time. (excercises) 1. Create a list_reverse function 2. Write a custom map function (list_map)
  • 12. Lightweight threads -module(simplethreads). -export([start/1]). hello(Text) -> io:format("Hello, ~p!~n", [Text]). start() -> spawn(simplethreads, hello, ["World"]). That should return PID of the spawned process, i.e. <0.59.0>
  • 13. Threads + Recursion = Awesomeness -module(threads). -export([start/0, say/2]). say(_, 0) -> done; say(Text, Times) -> io:format("~p~n", [Text]), say(Text, Times-1). start() -> spawn(threads, say, ["hello", 5]), spawn(threads, say, ["bye", 5]).
  • 14. Interprocess communication (1/2) -module(simplethreads). -export([start/0, say/0]). say() -> receive Text -> io:format("Hello, ~p!~n", [Text]) end. start() -> Pid = spawn(simplethreads, say, []), timer:sleep(200), Pid ! "World", ok.
  • 15. Interprocess communication (2/2.1) -module(pingpong). -export([start/0]). ping(0, PongPID) -> PongPID ! finished, io:format("ping finished~n", []); ping(N, PongPID) -> PongPID ! {ping, self()}, receive pong -> io:format("pong~n", []) end, ping(N-1, PongPID). % continued on the next slide
  • 16. Interprocess communication (2/2.2) pong() -> receive finished -> io:format("pong finished~n", []); {ping, PingPID} -> io:format("ping~n", []), PingPID ! pong, pong() end. start() -> PongPID = spawn(pingpong, pong, []), spawn(pingpong, ping, [5, PongPID]).
  • 17. Interprocess communication Eshell V7.1 (abort with ^G) 1> c(pingpong). {ok,pingpong} 2> pingpong:start(). ping <0.42.0> pong ping pong ping pong ping pong ping pong ping finished pong finished 3>
  • 18. OTP - Open Telecom Platform OTP stands for Open Telecom Platform, although it's not that much about telecom anymore (it's more about software that has the property of telecom applications, but yeah.) If half of Erlang's greatness comes from its concurrency and distribution and the other half comes from its error handling capabilities, then the OTP framework is the third half of it.
  • 19. OTP example -module(server). -behaviour(myserver). -export([ % The behaviour callbacks init/1, % - initializes our process handle_call/3, % - handles synchronous calls (with response) handle_cast/2, % - handles asynchronous calls (no response) handle_info/2, % - handles out of band messages (sent with !) terminate/2, % - is called on shut-down code_change/3]). % - called to handle code changes
  • 20. Elixir The new youth of Erlang
  • 21. What is Elixir? Elixir is a dynamic, functional language designed for building scalable and maintainable applications. Elixir leverages the Erlang VM, known for running low-latency, distributed and fault- tolerant systems, while also being successfully used in web development and the embedded software domain.
  • 22. The basics :hello # an atom "utf string ąę" # in erlang that would not be so easy hello = "a thing" # no longer capitalized, yay! hello = :hello # notice how we can overwrite the value IO.puts("hellonworld") length([1,2,3]) # I'll speak of lists in a second length [1,2,3] # notice how we can skip brackets
  • 23. Lists iex> a_list = [1,2,3,4,5] [1,2,3,4,5] iex> a_list = a_list ++ [6] [1,2,3,4,5,6] iex> a_list -- [1,3,5] [2,4,6] iex> [head|tail] = [1,2,3,4] # also hd/1, tl/1 [1,2,3,4] iex> head 1 iex> tail [2,3,4] iex> [104, 101, 108, 108, 111] "hello"
  • 24. Tuples iex> tuple = {:ok, "hello"} {:ok, "hello"} iex> put_elem(tuple, 1, "world") {:ok, "world"} iex> tuple # Elixir types are immutable {:ok, "hello"} iex> tuple_size tuple 2
  • 25. Modules and functions defmodule Calculator do def add(x, y) do x + y end end defmodule Lists do def reverse([head|tail], acc) do reverse(tail, [head|acc]) end # function clauses do not have to be distinguished def reverse([], acc) do acc end end
  • 26. If macro, keywords, maps and dicts iex> if false, do: :this, else: :that :that iex> if(false, [do: :this, else: :that]) :that iex> if(false, [{:do, :this}, {:else, :that}]) :that iex> map = %{:a => 1, :b => 2} %{a: 1, b: 2} iex> list = [a: 1, b: 3] [a: 1, b: 3] iex> Dict.put list, :a, 4 [a: 4, b: 3] iex> Dict.put map, :a, 4 %{a: 4, b: 2}
  • 27. Phoenix framework Phoenix is a web development framework written in Elixir which implements the server-side MVC pattern. Many of its components and concepts will seem familiar to those of us with experience in other web frameworks like Ruby on Rails or Python's Django.
  • 28. Getting started with Phoenix $ mix local.hex $ mix archive.install https://github.com/phoenixframework/phoenix/releases/download/v1.1.0/phoen $ mix phoenix.new hello $ mix ecto.create $ mix phoenix.server
  • 29. Bibliography The official "Getting started" guide - Learn You Some Erlang - Erlang Doc on distributed systems - OTP for beginners - Elixir Lang homepage - Phoenix Framework homepage - http://www.erlang.org/download/getting_started-5.4.pdf http://learnyousomeerlang.com/ link link http://elixir-lang.org/ http://www.phoenixframework.org/