The Erlang Programming Language

20
© ThoughtWorks 2008 The Erlang Programming Language Dennis Byrne - ThoughtWorks [email protected] http://notdennisbyrne.blogspot.com/

Transcript of The Erlang Programming Language

Page 1: The Erlang Programming Language

© ThoughtWorks 2008

The Erlang Programming Language

Dennis Byrne - [email protected]

http://notdennisbyrne.blogspot.com/

Page 2: The Erlang Programming Language

© ThoughtWorks 2008

Introduction

• Not a “shiny new object (or function)”• Open sourced by Ericsson• Functional Programming• Concurrency• Reliability• ThoughtWorks ! Erlang

Erlang is a functional programming language, with native constructs for concurrency and reliability.

Page 3: The Erlang Programming Language

© ThoughtWorks 2008

A Totally “Unbiased” Comparison

In Erlang: List = [1,2,3,4,5]. Doubled = lists:map( fun(I) -> I * 2 end, List).

In Java: List<Integer> list = new LinkedList<Integer>() {{

add(1); add(2); add(3); add(4); add(5); }}; List<Integer> doubled = new ArrayList<Integer>();

for(Integer integer : list) {doubled.add(integer.intValue() * 2);

}

Page 4: The Erlang Programming Language

© ThoughtWorks 2008

Single Assignment & Unification

I = 4. % assignmentI = 4. % unificationI = I. % unification4 = I. % unificationI = 5. % throws error :(

Page 5: The Erlang Programming Language

© ThoughtWorks 2008

Pattern Matching

1> MyList = [“A”]. % declaring a list2> [Var] = MyList. % implicit declaration 3> io:format(Var). % prints “A”

1> MyList = [“A”, “B”]. % declaring a list2> [_, Var] = MyList. % _ is ignored3> io:format(Var). % prints B

Page 6: The Erlang Programming Language

© ThoughtWorks 2008

Functions as First Class Citizens

1> Msg = "I am scoped".2> F = fun()->

io:format(Msg)end.

3> F(). % prints “I am scoped”

Page 7: The Erlang Programming Language

© ThoughtWorks 2008

Tail Recursion

Tail recursion is to ‘recursion’ as optimistic locking is to ‘locking’.

loop() ->io:format(“in constant space”),

loop().

• Automatic byte code manipulation• Last call optimization

Page 8: The Erlang Programming Language

© ThoughtWorks 2008

Pop Quiz: Evaluation Strategies

f( a(), b() )

• Order of evaluation• Lazy or strict?• Left, right, both, neither?

Page 9: The Erlang Programming Language

© ThoughtWorks 2008

State Erlang avoids complex solutions in favor of

simple ways to avoid the problem

• State is immutable w/ single assignment• State is private, passed by value• Changing Perspectives

– Contention vs. Scale– Side Effects vs. Scale

Page 10: The Erlang Programming Language

© ThoughtWorks 2008

Erlang Process

• An Erlang process combines the best of– An operating system process– An operating system thread– A “green thread”

• Controlled in user space by the ERTS• Costs less than 300 bytes

– Private heap– Private stack

• All processes are created equal• Incremental garbage collection

Page 11: The Erlang Programming Language

© ThoughtWorks 2008

The Actor Model

• Asynchronous Message passing• Messages passed by value, not reference• One to one relationship

– An Actor– A mailbox– A Process– A Process ID, or pid

Page 12: The Erlang Programming Language

© ThoughtWorks 2008

Concurrency Primitives: spawn

A built in function used to create a process.

Pid = spawn(Node, Module, Function, Args)

Page 13: The Erlang Programming Language

© ThoughtWorks 2008

Another “Unbiased” Comparison

In Erlang: Pid = spawn(fun() -> io:format(“hello world”) end).

In Java:Thread thread = new Thread() {

public void run() { System.out.println("Hello World");

} }.start();

Page 14: The Erlang Programming Language

© ThoughtWorks 2008

Concurrency Primitives: send

!

Page 15: The Erlang Programming Language

© ThoughtWorks 2008

Concurrency Primitives: send

The send operator passes a message to a process. It is “fire and forget”.

Pid ! Msg

Page 16: The Erlang Programming Language

© ThoughtWorks 2008

Concurrency Primitives: receive

receivePattern1 [ when Guard1 ] ->

dosomething().Pattern2 [ when Guard2 ] ->

dosomethingelse().end

Page 17: The Erlang Programming Language

© ThoughtWorks 2008

Reliability

• catch• try• Monitor

– monitor_node(Node, Bool)– Unidirectional

• Link– Pid = spawn_link(Node, Module, Fun, Args)– Bi-directional

Page 18: The Erlang Programming Language

© ThoughtWorks 2008

The Opposite of Ruby

Erlang Ruby

Orientation Functional Object

Syntax Ugly Beautiful

Concurrent Good Bad

Track Record Back end Front end

Execution Model Compiled Interpreted

Page 19: The Erlang Programming Language

© ThoughtWorks 2008

Erlang Servers and Frameworks

• Mnesia – distributed database• YAWS – “yet another web server”• ErlyWeb – MVC web framework• EUnit• Jungerl – third party libraries• Perferl ( http://code.google.com/p/perferl )

Page 20: The Erlang Programming Language

© ThoughtWorks 2008

The Erlang Programming Language

Dennis Byrne - [email protected]

http://notdennisbyrne.blogspot.com/