SlideShare a Scribd company logo
1 of 29
Download to read offline
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

What's hot

Introduction to Phoenix Framework (Elixir) 2016-01-07
Introduction to Phoenix Framework (Elixir) 2016-01-07Introduction to Phoenix Framework (Elixir) 2016-01-07
Introduction to Phoenix Framework (Elixir) 2016-01-07Svein Fidjestøl
 
Yaroslav Martsynyuk - Deploying Elixir/Phoenix with Distillery
Yaroslav Martsynyuk - Deploying Elixir/Phoenix with DistilleryYaroslav Martsynyuk - Deploying Elixir/Phoenix with Distillery
Yaroslav Martsynyuk - Deploying Elixir/Phoenix with DistilleryElixir Club
 
8 Minutes On Rack
8 Minutes On Rack8 Minutes On Rack
8 Minutes On Rackdanwrong
 
Phoenix for Rails Devs
Phoenix for Rails DevsPhoenix for Rails Devs
Phoenix for Rails DevsDiacode
 
maXbox Starter 42 Multiprocessing Programming
maXbox Starter 42 Multiprocessing Programming maXbox Starter 42 Multiprocessing Programming
maXbox Starter 42 Multiprocessing Programming Max Kleiner
 
Cloud meets Fog & Puppet A Story of Version Controlled Infrastructure
Cloud meets Fog & Puppet A Story of Version Controlled InfrastructureCloud meets Fog & Puppet A Story of Version Controlled Infrastructure
Cloud meets Fog & Puppet A Story of Version Controlled InfrastructureHabeeb Rahman
 
Ruby Concurrency and EventMachine
Ruby Concurrency and EventMachineRuby Concurrency and EventMachine
Ruby Concurrency and EventMachineChristopher Spring
 
Speech for Windows Phone 8
Speech for Windows Phone 8Speech for Windows Phone 8
Speech for Windows Phone 8Marco Massarelli
 
Découvrir dtrace en ligne de commande.
Découvrir dtrace en ligne de commande.Découvrir dtrace en ligne de commande.
Découvrir dtrace en ligne de commande.CocoaHeads France
 
Fluentd v0.12 master guide
Fluentd v0.12 master guideFluentd v0.12 master guide
Fluentd v0.12 master guideN Masahiro
 
Flask With Server-Sent Event
Flask With Server-Sent EventFlask With Server-Sent Event
Flask With Server-Sent EventTencent
 
Doing It Wrong with Puppet -
Doing It Wrong with Puppet - Doing It Wrong with Puppet -
Doing It Wrong with Puppet - Puppet
 
Async programming and python
Async programming and pythonAsync programming and python
Async programming and pythonChetan Giridhar
 
Exploring Async PHP (SF Live Berlin 2019)
Exploring Async PHP (SF Live Berlin 2019)Exploring Async PHP (SF Live Berlin 2019)
Exploring Async PHP (SF Live Berlin 2019)dantleech
 

What's hot (20)

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

Similar to Erlang and Elixir

Architecting Scalable Platforms in Erlang/OTP | Hamidreza Soleimani | Diginex...
Architecting Scalable Platforms in Erlang/OTP | Hamidreza Soleimani | Diginex...Architecting Scalable Platforms in Erlang/OTP | Hamidreza Soleimani | Diginex...
Architecting Scalable Platforms in Erlang/OTP | Hamidreza Soleimani | Diginex...Hamidreza Soleimani
 
Origins of Elixir programming language
Origins of Elixir programming languageOrigins of Elixir programming language
Origins of Elixir programming languagePivorak MeetUp
 
Erlang Message Passing Concurrency, For The Win
Erlang  Message  Passing  Concurrency,  For  The  WinErlang  Message  Passing  Concurrency,  For  The  Win
Erlang Message Passing Concurrency, For The Winl xf
 
Introducing Elixir and OTP at the Erlang BASH
Introducing Elixir and OTP at the Erlang BASHIntroducing Elixir and OTP at the Erlang BASH
Introducing Elixir and OTP at the Erlang BASHdevbash
 
Introduction To Erlang Final
Introduction To Erlang   FinalIntroduction To Erlang   Final
Introduction To Erlang FinalSinarShebl
 
What I learned from Seven Languages in Seven Weeks (IPRUG)
What I learned from Seven Languages in Seven Weeks (IPRUG)What I learned from Seven Languages in Seven Weeks (IPRUG)
What I learned from Seven Languages in Seven Weeks (IPRUG)Kerry Buckley
 
Erlang is not a city in Germany
Erlang is not a city in GermanyErlang is not a city in Germany
Erlang is not a city in Germanymomo-13
 
Erlang
ErlangErlang
ErlangESUG
 
Programming Under Linux In Python
Programming Under Linux In PythonProgramming Under Linux In Python
Programming Under Linux In PythonMarwan Osman
 
Erlang kickstart
Erlang kickstartErlang kickstart
Erlang kickstartRyan Brown
 
Erlang bootstrap course
Erlang bootstrap courseErlang bootstrap course
Erlang bootstrap courseMartin Logan
 
Elixir formatter Internals
Elixir formatter InternalsElixir formatter Internals
Elixir formatter InternalsPedro Medeiros
 

Similar to Erlang and Elixir (20)

Elixir introduction
Elixir introductionElixir introduction
Elixir introduction
 
Architecting Scalable Platforms in Erlang/OTP | Hamidreza Soleimani | Diginex...
Architecting Scalable Platforms in Erlang/OTP | Hamidreza Soleimani | Diginex...Architecting Scalable Platforms in Erlang/OTP | Hamidreza Soleimani | Diginex...
Architecting Scalable Platforms in Erlang/OTP | Hamidreza Soleimani | Diginex...
 
Elixir
ElixirElixir
Elixir
 
Origins of Elixir programming language
Origins of Elixir programming languageOrigins of Elixir programming language
Origins of Elixir programming language
 
Erlang, an overview
Erlang, an overviewErlang, an overview
Erlang, an overview
 
Erlang Message Passing Concurrency, For The Win
Erlang  Message  Passing  Concurrency,  For  The  WinErlang  Message  Passing  Concurrency,  For  The  Win
Erlang Message Passing Concurrency, For The Win
 
Elixir cheatsheet
Elixir cheatsheetElixir cheatsheet
Elixir cheatsheet
 
Introducing Elixir and OTP at the Erlang BASH
Introducing Elixir and OTP at the Erlang BASHIntroducing Elixir and OTP at the Erlang BASH
Introducing Elixir and OTP at the Erlang BASH
 
Introduction To Erlang Final
Introduction To Erlang   FinalIntroduction To Erlang   Final
Introduction To Erlang Final
 
What I learned from Seven Languages in Seven Weeks (IPRUG)
What I learned from Seven Languages in Seven Weeks (IPRUG)What I learned from Seven Languages in Seven Weeks (IPRUG)
What I learned from Seven Languages in Seven Weeks (IPRUG)
 
Erlang is not a city in Germany
Erlang is not a city in GermanyErlang is not a city in Germany
Erlang is not a city in Germany
 
Elixir and OTP Apps introduction
Elixir and OTP Apps introductionElixir and OTP Apps introduction
Elixir and OTP Apps introduction
 
Erlang
ErlangErlang
Erlang
 
Programming Under Linux In Python
Programming Under Linux In PythonProgramming Under Linux In Python
Programming Under Linux In Python
 
An introduction to Ruby
An introduction to RubyAn introduction to Ruby
An introduction to Ruby
 
Erlang kickstart
Erlang kickstartErlang kickstart
Erlang kickstart
 
Python 101 1
Python 101   1Python 101   1
Python 101 1
 
Erlang bootstrap course
Erlang bootstrap courseErlang bootstrap course
Erlang bootstrap course
 
Elixir formatter Internals
Elixir formatter InternalsElixir formatter Internals
Elixir formatter Internals
 
Erlang
ErlangErlang
Erlang
 

Recently uploaded

A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)Gabriella Davis
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfEnterprise Knowledge
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking MenDelhi Call girls
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...Neo4j
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Servicegiselly40
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘RTylerCroy
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024Rafal Los
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdfhans926745
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Drew Madelung
 
Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Allon Mureinik
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountPuma Security, LLC
 
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxFactors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxKatpro Technologies
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Miguel Araújo
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreternaman860154
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Scriptwesley chun
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024The Digital Insurer
 
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j
 
Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesUnblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesSinan KOZAK
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptxHampshireHUG
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationRadu Cotescu
 

Recently uploaded (20)

A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path Mount
 
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxFactors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreter
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
 
Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesUnblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen Frames
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 

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/