Erlang

21
Erlang

Transcript of Erlang

Page 1: Erlang

Erlang

Page 2: Erlang

What is it?

created by Joe Armstrong in 1986 at Ericcsson Telecom

based on Prolog

functional language

dynamicly typed

Page 3: Erlang

Erlang is...

reliable

high concurrent (no threads. light processes)

modular

Page 4: Erlang

Where is it used?

RabbitMQ (France telecom)

CouchDB

Facebook Chat

GitHub (egitd)

SimpleDB (AWS)

Page 5: Erlang

Types

Page 6: Erlang

Variables

Var = 2.

Var.%=> 2

Var = 3. % this throws an error!

Page 7: Erlang

atoms

this_is_atom

Page 8: Erlang

Lists

[1,2,3,4]

Page 9: Erlang

Tuples

{a, 2,"d"}.

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

Page 10: Erlang

Matching

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

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

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

Page 11: Erlang

Matching

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

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

Page 12: Erlang

Matching

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

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

Page 13: Erlang

functions

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

mirror(Argument) -> Argument.

Page 14: Erlang

functions

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

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

Page 15: Erlang

functions

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

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

Page 16: Erlang

functions

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

Page 17: Erlang

Control structures

case...

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

Page 18: Erlang

Control structures

if...

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

Page 19: Erlang

Processes

Page 20: Erlang

Processes

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

Pid ! "message".

Page 21: Erlang

Processes

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

_ -> io::format("Whatever..."), function_name()

end.