Erlang

17
Erlang – What is it? - created by Joe Armstrong in 1986 at Ericcsson Telecom - based on Prolog - functional language - dynamicly typed Author: Mateusz Zawisza

Transcript of Erlang

Page 1: Erlang

Erlang – What is it?

- created by Joe Armstrong in 1986 at Ericcsson Telecom- based on Prolog

- functional language- dynamicly typed

Author: Mateusz Zawisza

Page 2: Erlang

Where is it used?

- RabbitMQ (France Telecom)- Facebook Chat- GitHub (egitd)

- SampleDB (AWS)

Author: Mateusz Zawisza

Page 3: Erlang

Types - variables

Var = 2.Var. %=> 2Var = 3. % this throws an error!

Author: Mateusz Zawisza

Page 4: Erlang

Types - atoms

- same role as symbols in Ruby- usually start with lower-case

this_is_atom

Author: Mateusz Zawisza

Page 5: Erlang

Types - lists

[1, 2, 3, 4]

Author: Mateusz Zawisza

Page 6: Erlang

Types - tuples

{a, 2,"d"}.{ company, {name, "Applicake",

{address, "Krakow"}}

}.

Author: Mateusz Zawisza

Page 7: Erlang

MatchingCompany = {company, {name, "Applicake"}, {address, "Krakow"} }.

{company, {name, Name}, {address, Address}} = Company.

Name %=> "Applicake" Address %=> "Krakow"

Author: Mateusz Zawisza

Page 8: Erlang

Matching

[Head | Tail] = [1,2,3,4].

Head. %=> 1 Tail. %=> [2,3,4]

Author: Mateusz Zawisza

Page 9: Erlang

Matching

[One, Two | Rest] = [1,2,3,4].

One. %=> 1 Two. %=> 2 Rest.%=> [3,4]

Author: Mateusz Zawisza

Page 10: Erlang

Functions

-module(mirror_function). -export([mirror/1]).

mirror(Argument) -> Argument.

Author: Mateusz Zawisza

Page 11: Erlang

Functions

-module(matching_function). -export([number/1]).

number(one) -> 1; number(two) -> 2; number(three) -> 3.

Author: Mateusz Zawisza

Page 12: Erlang

Functions

Numbers = [1,2,3,4].

lists:map(fun(X) -> X+1 end, Numbers). %=> [2,3,4,5]

Author: Mateusz Zawisza

Page 13: Erlang

Functions

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

Author: Mateusz Zawisza

Page 14: Erlang

Control sturctures...

case...

case Animal of "dog" -> underdoga; "cat" -> thundercat _ -> something_elseend.

Author: Mateusz Zawisza

Page 15: Erlang

Control sturctures... if...

if X > 0 -> positive; X < 0 -> negative; true -> zeroend.

Author: Mateusz Zawisza

Page 16: Erlang

Processes

Pid = spawn(fun module_name:function_name/0).

Pid ! "message".

Author: Mateusz Zawisza

Page 17: Erlang

Processes

fuction_name() -> receive "message" -> io:format("Hi!"), function_name(); _ -> io:format("Whatever..."), function_name()end.

Author: Mateusz Zawisza