Top Banner
@alepoletto
20

Erlang

May 25, 2015

Download

Technology

a extended version of a previous lighting's talk, i presented this in a functional society meeting.
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Erlang

@alepoletto

Page 2: Erlang

What is/ Why?• Functional programming language.

• Large emphasis on concurrency and high reliability

• Erlang's runtime system has built-in support for concurrency,

distribution and fault tolerance

• Use the Actor Model

• Used in telecoms, banking, e-commerce, computer telephony and

instant messaging.

• Runs inside a virtual machine.

@alepoletto

Page 3: Erlang

Who created?

@alepoletto

Page 4: Erlang

Reference to A. K. Erlang

@alepoletto

Page 5: Erlang

Who is using

@alepoletto

Page 6: Erlang

@alepoletto

Page 7: Erlang

Variables and Atoms

• X = 2 + 2.

• X = 4.

• X = 7.

• x = 4. (can’t be lower case)

• Lower case is reserved to atoms

• Lets say that car is a atom.

• Them car is a car nothing more.

• Useful to express values

@alepoletto

Page 8: Erlang

Functions

soma(A, B) -> A+B.

ola() -> io:format(“Ola”).

olaSoma(A) -> ola(), soma(A,1).

@alepoletto

Page 9: Erlang

Modules

-module(society).

-export([soma/2]).

-compile(export_all).

soma(A, B) -> A+B.

ola() -> io:format(“Ola”).

@alepoletto

Page 10: Erlang

Pattern Matcher

@alepoletto

Page 11: Erlang

Pattern Matching

function jovem(Pessoa)

if Pessoa == chaves then

print(“Jovem ainda”)

else if Pessoa == madruga then

print(“Velho”)

else

print(“Não sei")

end

@alepoletto

Page 12: Erlang

Pattern Matching

jovem(chaves) -> io:format(“Jovem ainda”);

jovem(madruga) -> io:format(“Velho”);

jovem(_) -> io:format(“Não Sei”).

adulto(X) when X >= 21 -> true;

adulto(_) -> false.

@alepoletto

Page 13: Erlang

Lets be Functional

fatorial(0) -> 1;

fatorial(N) -> when N > 0 -> N * fatorial(N-1).

map(_, []) -> [];

map(F, [H|T]) -> [F(H)|map(F,T)].

L = [1,2,3,4,5].

map(fun society:fatorial/1, L).

map(fun(X) -> X + 1 end, L).

@alepoletto

Page 14: Erlang

Actors

@alepoletto

Page 15: Erlang

Actors

cobra() ->receive

{From, speak} ->From ! “You're the disease, and I'm the

cure.";{From, deal} ->

From ! “I don't deal with psychos. I put them away. ";

_ ->io:format(“no talk")

end.

@alepoletto

Page 16: Erlang

Actors

Stallone = spawn(society, cobra, []).

<0.55.0>

Stallone! {self(), speak}.

flush().

"You're the disease, and I'm the cure. “

Stallone! {self(), danca}.

flush().

@alepoletto

Page 17: Erlang

Actors

cobra() ->receive

{From, speak} ->From ! “You're the disease, and I'm the cure."cobra();

{From, deal} ->From ! “I don't deal with psychos. I put them away. "cobra();

_ ->io:format(“no talk")cobra();

end.

@alepoletto

Page 18: Erlang

Actors

Stallone! {self(), speak}.

“You're the disease, and I'm the cure.“

Stallone! {self(), deal}.

“I don't deal with psychos. I put them away. “

Stallone! {self(), shoot}.

“no talk”

@alepoletto

Page 19: Erlang

Reference

• http://learnyousomeerlang.com/

• http://www.erlang.org/

• http://en.wikipedia.org/wiki/Erlang_(programming_language)

@alepoletto

Page 20: Erlang

Thank You

@alepoletto