5. Syntax: Atom
1> atom.
atom
2> anythingStartingWithLowercaseLetter.
anythingStartingWithLowercaseLetter
3> 'anything enclosed in single quotes'.
'anything enclosed in single quotes'
11. Pattern Matching
1> 1 = 1.
1
2> 1 = 2.
** exception error: no match of right hand side value 2
3> catch 1 = 2.
{'EXIT',{{badmatch,2},[{erl_eval,expr,3}]}}
12. Pattern Matching: Binding Variables
1> X.
* 1: variable 'X' is unbound
2> X = 5.
5
3> X.
5
4> X = 6.
** exception error: no match of right hand side value 6
13. Pattern Matching: Destructuring
1> Coder = { coder, { name, quot;Gabrielequot; } }.
{coder,{name,quot;Gabrielequot;}}
2> { person, { name, PersonName } } = Coder.
** exception error: no match of right hand side value {coder,
{name,quot;Gabrielequot;}}
3> { coder, { name, quot;Gabrielequot; } } = Coder.
{coder,{name,quot;Gabrielequot;}}
4> { coder, { name, CoderName } } = Coder.
{coder,{name,quot;Gabrielequot;}}
14. Pattern Matching: Destructuring
7> AnotherCoderName = quot;Matteoquot;.
quot;Matteoquot;
8> { coder, { name, AnotherCoderName } } = Coder.
** exception error: no match of right hand side value {coder,
{name,quot;Gabrielequot;}}
15. Pattern Matching: List
1> [ First, Last ] = [ first, last ].
[first,last]
2> First.
first
3> Last.
last
18. Pattern Matching: In Deep
1> { X, Y, X } = { { abc, 12 }, 42, { abc, 12 } }.
{{abc,12},42,{abc,12}}
2> X.
{abc,12}
3> Y.
42
19. Pattern Matching: In Deep
1> { X, Y, X } = { { abc, 12 }, 42, true }.
** exception error: no match of right hand side value {{abc,
12},42,true}
2> { X, Y, _ } = { { abc, 12 }, 42, true }.
{{abc,12},42,true}
31. Syntax: List Comprehension
triplets(UpTo) when is_number(UpTo), UpTo >= 2 -> [
{ A, B, C } ||
A <- lists:seq(2, UpTo),
B <- lists:seq(2, UpTo),
C <- lists:seq(2, UpTo),
A * A + B * B =:= C * C
].
1> examples:triplets(10).
[{3,4,5},{4,3,5},{6,8,10},{8,6,10}]
33. List Functions: Fold/Reduce
sum(Numbers) ->
lists:foldl(fun
(Number, Sum) when is_integer(Number) ->
Sum + Number;
(_Number, Sum) ->
Sum
end, 0, Numbers).
1> examples:sum([ 1, 2, 3, foo, 4, bar, 5 ]).
15
34. Example: FizzBuzz
fizzbuzz(UpTo) ->
lists:map(fun(Counter) ->
if
(Counter rem 15) =:= 0 -> fizzbuzz;
(Counter rem 3) =:= 0 -> fizz;
(Counter rem 5) =:= 0 -> buzz;
true -> Counter
end
end, lists:seq(1, UpTo)).
1> examples:fizzbuzz(16).
[1,2,fizz,4,buzz,fizz,7,8,fizz,buzz,11,fizz,13,14,fizzbuzz,16]
35. Process: Concurrency
• Any function (named or anonymous) can
become a process
• A process is simply a function executing in
parallel
• A process shares nothing with other processes
• A process is extremely lightweight: a modern
systems can accommodate literally millions of
Erlang processes
36. Process: Concurrency
1> processes:profileSpawnFor(1000).
Spawned 1000 processes in 10 (4) milliseconds
ok
2> processes:profileSpawnFor(10000).
Spawned 10000 processes in 40 (96) milliseconds
ok
3> processes:profileSpawnFor(100000).
Spawned 100000 processes in 510 (884) milliseconds
ok
38. Process: Concurrency
• The “!” operator sends messages
• Asynchronous
• Order guaranted
• The “receive” statement matches messages
• One call to receive, one message removed
from the process message queue
• Uses pattern matching to select messages
39. Process: Concurrency
1> Pid = spawn(fun() ->
1> receive
1> die -> io:format(quot;Bye~nquot;)
1> end
1> end).
<0.33.0>
42. Process: Fault Tollerance
• The “link” function can link processes
• When a process die the linked processes
receive a message { ‘EXIT’, Died, Reason }
• A monitor process could respawn the died
ones or cleanup the system
43. Process: Distributed
• The spawn(Node, Function) can spawn a
process in any other known nodes
• No other changes are required!!!
44. Hard Gained Insights
• Don’t miss tail-call recursion
• Asynchronous is good
• Small functions are good
• When in doubt create a new process
• Think early what to do when a process die
45.
46. Disco
(Map/Reduce with
Erlang + Python)
Yaws
(Yet Another Web Server)
eJabbered
(jabber/XMPP)