31
Introduc)on to Erlang Abd El‐Fa3ah Hussein Mahran

Introduction To Erlang Final

Embed Size (px)

DESCRIPTION

 

Citation preview

Page 1: Introduction To Erlang   Final

Introduc)on to Erlang           Abd El‐Fa3ah Hussein Mahran 

Page 2: Introduction To Erlang   Final

Agenda

  History of Erlang   Erlang Features   Erlang/OTP and design patterns   Tools applications   Applications written in Erlang   Erlang IDEs   Erlang syntax   How to get Erlang   Demo   Conclusion   Q&A

Page 3: Introduction To Erlang   Final

History of Erlang

  Erlang is a programming language

  Ericsson wanted programming language for developing telecommunication switching systems.

  The language must be a very high level symbolic language in order to achieve productivity gains

  The language must contain primitives for concurrency and error recovery

Page 4: Introduction To Erlang   Final

1984: Ericsson Computer Science Lab formed

1984-86: Experiments programming POTS with several languages

1998: Open Source Erlang

1987: Early Erlang Prototype projects

1991: First fast implementation

1993: Distributed Erlang

1995: Several new projects

1996: Open Telecom Platform (research on verification...)‏

No language well suited for telecom systems

development

History of Erlang

Page 5: Introduction To Erlang   Final

Erlang is Concurrent functional programming language

It was designed by Ericsson to support distributed, fault-tolerant, soft-real-time, and non-stop applications

Page 6: Introduction To Erlang   Final

Erlang Features

  Concurrency   Fault tolerance   Soft Real-Time   Distribution   Hot Code Loading   External Interfaces   Platform Independent

Page 7: Introduction To Erlang   Final

Erlang/OTP and design patterns

  Gen_Server Behaviour   Gen_Fsm Behaviour   Gen_Event Behaviour   Supervisor Behaviour   Releases   Target systems

Page 8: Introduction To Erlang   Final

Tools applications   Dialyzer   Eunit   Edoc   Common_test   Test_server   Jinterface   Erl_interface   wx   Ssh   Ssl   xmerl

Depending on C++ WXwidgets

Page 9: Introduction To Erlang   Final

Applications written in Erlang

Page 10: Introduction To Erlang   Final

Applications written in Erlang   Ericsson Company

  Bluetail/Alteon/Nortel (distributed, fault tolerant email system, SSL accelerator)

  Cellpoint (Location-based Mobile Services)

  Corelatus (SS7 monitoring).

  dqdp.net (in Latvian) (Web Services).

  Facebook (Facebook chat backend)

  Finnish Meteorological Institute (Data acquisition and real-time monitoring)

  IDT corp. (Real-time least-cost routing expert systems)

  Kreditor (Electronic payment systems)

  Mobilearts (GSM and UMTS services)

  Netkit Solutions (Network Equipment Monitoring and Operations Support Systems)

  Process-one (Jabber Messaging)

  Schlund + Partner (Messaging and Interactive Voice Response services)

  Quviq (Software Test Tool)

  RabbitMQ (AMQP Enterprise Messaging)

  T-Mobile (previously one2one) (advanced call control services)

  Telia (a telecomms operator)

  Vail Systems (Interactive Voice Response systems)

  Wavenet (SS7 and IVR applications)

Page 11: Introduction To Erlang   Final

Erlang IDEs

  Eclipse (ErlIDE plugin)   NetBeans (ErlyBird)   Emacs   VIM   Any other editor (Gedit, Notepad++)

Page 12: Introduction To Erlang   Final

Erlang syntax

  Data types   If statement   Case statement   Functions   Modules   Hello World program   How Erlang works

Page 13: Introduction To Erlang   Final

Data types   Number

  Integer 12, 43   Float 34.3

  Atoms create, remove   Binaries <<“hello”>>   Reference   Fun   Port Identifier   Pid <0,36,0>   Tuple {1, 2, 3}   List [1, 2, 3, 4]   String “hello”   Record -record(person, {name, age, phone}).

Erlang don’t have boolean as data

type, instead Erlang has true and false

Page 14: Introduction To Erlang   Final

If statement

if GuardSeq1 -> Body1; ...; GuardSeqN -> BodyN end.

Page 15: Introduction To Erlang   Final

Case Statement

case Expr of Pattern1 [when GuardSeq1]-> Body1; ...; PatternN [when GuardSeqN] -> BodyN end.

Page 16: Introduction To Erlang   Final

Functions

Name(Pattern11,...,Pattern1N) [when GuardSeq1] -> Body1;

...; Name(PatternK1,...,PatternKN) [when GuardSeqK] -> ….., BodyK.

Function name Parameters

Body

Conditions

Clause 1

Clause 2

Page 17: Introduction To Erlang   Final

Modules -module(moduleName).

-export([func/0, func/1]).

func() -> foo(5), … .

func(A) -> foo(A), … .

foo(N) -> …, …, ...

Page 18: Introduction To Erlang   Final

Hello world Program

-module(hello_world).

-export([print/0]).

print() -> io:format(“Hello World…!~n”).

$ erl Eshell V 5.7.1 (abort with ^G) 1> c(hello_world). {ok, hello_world} 2> hello_world:print(). Hello World…! 3>

Save the file as “hello_world.erl”, After compilation output will be “hello_world.beam”

Page 19: Introduction To Erlang   Final

How Erlang works   Pattern matching technique

  Var = 3.   [Var1, Var2] = [3, 4].   {Var3, _Var4, Var5} = {4, 5, a}.   [H | T] = [iti, erlang, course, book].

1 = 1. {ok, Var} = {ok, “Connected to DB”}.

1 = 2. {ok, var} = {ok, “Connected to DB”}.

X

Page 20: Introduction To Erlang   Final

Recursion and Tail Recursion

fact(0) -> 1; fact(N) when N>0 -> N * fact(N -1).

fact(N) -> fact_help(N, 1).

fact_help(0, Acc) -> Acc; fact_help(N, Acc) when N>0 -> NewAcc = N * Acc, fact_help(N-1, NewAcc).

Page 21: Introduction To Erlang   Final

Quick Sort

quicksort([]) -> % If the list [] is empty, return an empty list (nothing to sort)

[]; quicksort([Pivot|Rest]) -> quicksort([Front || Front <- Rest, Front < Pivot]) ++ [Pivot] ++ quicksort([Back || Back <- Rest, Back >= Pivot]).

Page 22: Introduction To Erlang   Final

How to get Erlang

  Erlang.org   Download   Documentation   Trapexit.org   Erlang Factory   Erlang-Consulting

Page 23: Introduction To Erlang   Final

Demo

  Hot Code swapping   Concurrency   Fault tolerance   Distributed application

Page 24: Introduction To Erlang   Final

Hot Code swapping

-module(changing_code).

-export([start/0, loop/0]).

start() -> spawn(changing_code, loop, []).

loop() -> receive switch ->

changing_code:loop(); hello ->

io:format(“CAT Hackers ----1~n”), loop();

Msg -> io:format(“~p~n”, [Msg]), loop(),

end.

% Forces the use of loop/0 from

the latest version of changing_code

Page 25: Introduction To Erlang   Final

Concurrency

portscan(Ip, From, To) when From < To -> io:format("Ports open for: ~p:~n", [Ip]), pforeach(fun(Port) -> scan(Ip,Port) end,lists:seq(From, To)).

scan(Ip, Port) -> case gen_tcp:connect(Ip,Port,[{active, false}, binary]) of {ok,P} ->

gen_tcp:close(P), io:format("~p is open~n", [Port]); _ -> ok

end.

pforeach(F, [H|T]) -> spawn(fun() -> F(H) end), pforeach(F, T);

pforeach(_ , []) -> ok.

Page 26: Introduction To Erlang   Final

Fault tolerance

init([]) -> AChild= {main_module,{main_module, start_link, []}, permanent,

2000, worker, [main_module]}, {ok,{{one_for_one, 1, 60}, [AChild]}}.

Page 27: Introduction To Erlang   Final

Distributed application

[{kernel, [{distributed, [{dist_app, 5000, [‘[email protected]’, {‘[email protected]’, ‘[email protected]’}]}]}, {sync_nodes_mandatory, [‘[email protected]’, ‘[email protected]’]}, {sync_nodes_timeout, 5000} ] } ].

Timeout to wait until restart application on

other node

Nodes

Specifies how many milliseconds to wait for

the other nodes to start

Page 28: Introduction To Erlang   Final

Conclusion   Erlang is concurrent functional programming

language.   Its main goal is error recovery, fault tolerant,

distributed applications, and non-stop applications.   It released to Open source since 1998.   Companies that are using Erlang.   How Erlang works.   How to get and use Erlang.

Page 29: Introduction To Erlang   Final

References

  Erlang.org   Trapexit.org   Programming Erlang, Joe Armstong   Wikipedia.com

Page 30: Introduction To Erlang   Final

Q&A

Page 31: Introduction To Erlang   Final

Thanks