Erlang Webcast
Erlang Webcast
http://paulbarry.itcarlow.ie/ErlangWebcast.pdf
2
Disclaimer
3
What exactly is Erlang?
4
“Erlang is a declarative,dynamically-typed,
functional, concurrent, distributed and
fault-tolerant programming language
with garbage collection and code
hot-swapping built into its runtime.”
Buzz-word city,
dude!
5
Scratching an itch in
the early '80s...
6
Why learn Erlang?
7
“Learn a new programming
language every year”
8
“Buy at least one new
programming book every year”
9
So, really, why learn Erlang?
10
A better programmer,
you will be...
11
Learn how others solve problems with a
different language, then apply these new
techniques to your current language
12
Learn what each language is good at,
then pick the right tool for the job
13
Works with a really big hammer!
14
What is Erlang good at?
15
What Erlang Wasn't Designed To Do
16
Erlang wasn't designed to...
Run in a browser
Process text efficiently
Be easy to teach
Be easy to learn
Build dynamic websites
Run on mobile phones
Allow non-programmers to program
Build GUIs
17
Erlang was designed to build
software systems that never stop
18
What exactly do you
mean by “never”?
19
“Never” at Ericsson means “no more
than 4 minutes downtime per year”...
http://www.infoq.com/presentations/Systems-that-Never-Stop-Joe-Armstrong 20
Wow! That's 5 nines
availability!*
21
* 99.999% uptime
Let's build software
that's 100% defect free!
22
Let's throw lots and lots
of code at the problem and
it'll go away, eh?
23
There has to be a better way
24
Let it crash!
25
Robustness is achieved by having
programmers concentrate on processes and the
interactions (or messages) between them
26
Early error detection/recovery and the use
of concurrent programming techniques
lets you build fault tolerant systems
27
COP: Concurrency-Oriented Programming
28
The problem is...
29
The problem is...
30
Consider this code...
-module(hello_server).
-export([hello/0]).
hello() ->
receive
case Who of
end,
hello()
end.
31
Strange punctuation symbols . ; ,
-module(hello_server).
-export([hello/0]).
hello() ->
receive
case Who of
end,
hello()
end.
32
Lots of arrows -> in lots of places
-module(hello_server).
-export([hello/0]).
hello() ->
receive
case Who of
end,
hello()
end.
33
Even stranger symbols - _ !
-module(hello_server).
-export([hello/0]).
hello() ->
receive
case Who of
end,
hello()
end.
34
What's the deal with [] {} and () ?
-module(hello_server).
-export([hello/0]).
hello() ->
receive
case Who of
end,
hello()
end.
35
Functions that call themselves
-module(hello_server).
-export([hello/0]).
hello() ->
receive
case Who of
end,
hello()
end.
36
This is weird...
37
Erlang Culture Shock
38
Shock #1
-module(howdy).
-export([hi/1]).
hi(Name) ->
io:format('Hi there, ~p!~n', [Name]).
howdy:hi(Last).
39
Shock #2
40
Shock #3
EVERYTHING
41
So... this doesn't work!
X = 10.
X = X + 1.
Y = X + 1.
42
Erlang's troublesome = operator
43
No side effects here!
X
Pid
Func
44
Shock #4
45
So... how do you loop?
46
List Comprehensions
Erlang:
Alist = [1, 2, 3, 4, 5].
Doubled = [X*2 || X <- Alist].
Python:
alist = [1, 2, 3, 4, 5]
doubled = [x*2 for x in alist]
Note: alist and doubled are mutable; Alist and Doubled are not
47
Shock #5
48
Spot the loop?
-module(hello_server).
-export([hello/0]).
hello() ->
receive
case Who of
end,
hello()
49
end.
Shock #6
50
Sweet mother of all
things Erlang! Whose bright
idea was that?
51
Shock #7
There is no
statement
52
There are if and case expressions
but... they're kinda weird
53
54
With enough pig-headed persistence,
weirdness can be overcome
55
So... who do I call to
help me learn Erlang?
56
57
58
Erlang is a language you study
as getting up-to-speed with
Erlang takes time
59
Web Resources
http://www.erlang.org
Try Erlang:
http://www.tryerlang.org/
“Learn Some Erlang For Great Good!”:
http://learnyousomeerlang.com/
The Erlang Factory:
http://erlang-factory.com/
InfoQ:
http://www.infoq.com
60
Some Unique “Characteristics”
61
Banned by Ericsson in 1998 for
“not being open enough”
62
63
http://video.google.com/videoplay?docid=-5830318882717959520
64
This is all great, but...
how exactly do I use
Erlang to build a
fault-tolerant system?
65
Learn Three Things
Create an Erlang process with spawn()
Send a message to a process with !
Process a message with receive
66
Remember this code?
%% Saved in 'hello_server.erl'.
-module(hello_server).
-export([hello/0]).
hello() ->
receive
case Who of
end,
hello()
end. 67
Create the hello() process
<0.38.0>
68
Send a message to a process with !
Pid ! robert.
robert
69
A little twist
{<0.31.0>,robert}
70
Messages sent to a process with !
are received by receive
71
Can you spot the pattern match?
%% Saved in 'hello_server.erl'.
-module(hello_server).
-export([hello/0]).
hello() ->
receive
case Who of
end,
hello()
end. 72
The {FromPID, Who} tuple matches
%% Saved in 'hello_server.erl'.
-module(hello_server).
-export([hello/0]).
hello() ->
receive
case Who of
end,
hello()
end. 73
Send a message back...
%% Saved in 'hello_server.erl'.
-module(hello_server).
-export([hello/0]).
hello() ->
receive
case Who of
end,
hello()
end. 74
spawn, send (!) then receive
"Hello Robert."
75
Things get really interesting
when the processes are
linked together with spawn_link()
and the trap_exit signal
76
Processes that are linked together can
react appropriately to EXIT messages from
each other... and what happens next is
controlled by the programmer
77
Distributed Erlang
I need to spawn
hello/0 hello/0 is
on glasnost... registered
pbmac.itcarlow.ie as 'hr'
78
glasnost.itcarlow.ie
Pid = spawn(fun hello_server:hello/0),
becomes
Pid = spawn(‘[email protected]’, hello_server, hello, []),
79
Over time, you'll end up repeating
a lot of your process management code...
80
81
OTP is the jewel in Erlang's crown
82
So... if Erlang's so
cool, how come no one
is using it?
83
The TIOBE Index
84
85
Erlang in Telecoms
86
Erlang in Data
87
Erlang on the Web
88
Erlang in Gaming
89
Check out Bob Ippolito’s Erlang-Factory talks!
More Erlang in Gaming
90
Thanks to Malcolm Dowse (of DemonWare, Dublin) for permission to use these
images, which were first delivered as part of Malcolm's talk to Erlang Factory London 2011.
So... is Erlang
worth learning?
91
Yes
92
Lots more to discover...
Fun with fun
Bit-strings and bit syntax
ETS and DETS
“Live” code updating
The Mnesia Distributed Database
The Standard Library
CEAN
93
Friendly Community
94
Questions?
95