The Erlang Programming Language

2,727 views
2,525 views

Published on

Published in: Technology, News & Politics
1 Comment
2 Likes
Statistics
Notes
No Downloads
Views
Total views
2,727
On SlideShare
0
From Embeds
0
Number of Embeds
23
Actions
Shares
0
Downloads
92
Comments
1
Likes
2
Embeds 0
No embeds

No notes for slide

The Erlang Programming Language

  1. 1. The Erlang Programming Language Dennis Byrne - ThoughtWorks [email_address] http://notdennisbyrne.blogspot.com/ © ThoughtWorks 2008
  2. 2. Introduction <ul><li>Not a “shiny new object (or function)” </li></ul><ul><li>Open sourced by Ericsson </li></ul><ul><li>Functional Programming </li></ul><ul><li>Concurrency </li></ul><ul><li>Reliability </li></ul><ul><li>ThoughtWorks ! Erlang </li></ul>© ThoughtWorks 2008 Erlang is a functional programming language, with native constructs for concurrency and reliability.
  3. 3. A Totally “Unbiased” Comparison <ul><li>In Erlang: </li></ul><ul><li>List = [1,2,3,4,5]. </li></ul><ul><li>Doubled = lists:map( fun(I) -> I * 2 end, List). </li></ul><ul><li>In Java: </li></ul><ul><li>List<Integer> list = new LinkedList<Integer>() {{ </li></ul><ul><li>add(1); add(2); add(3); add(4); add(5); </li></ul><ul><li>}}; </li></ul><ul><li>List<Integer> doubled = new ArrayList<Integer>(); </li></ul><ul><li>for(Integer integer : list) { </li></ul><ul><li>doubled.add(integer.intValue() * 2); </li></ul><ul><li>} </li></ul>© ThoughtWorks 2008
  4. 4. Single Assignment & Unification <ul><li>I = 4. % assignment </li></ul><ul><li>I = 4. % unification </li></ul><ul><li>I = I. % unification </li></ul><ul><li>4 = I. % unification </li></ul><ul><li>I = 5. % throws error :( </li></ul>© ThoughtWorks 2008
  5. 5. Pattern Matching <ul><li>1> MyList = [ “ A ” ]. % declaring a list </li></ul><ul><li>2> [Var] = MyList. % implicit declaration </li></ul><ul><li>3> io:format(Var). % prints “ A ” </li></ul><ul><li>1> MyList = [ “ A ” , “ B ” ]. % declaring a list </li></ul><ul><li>2> [_, Var] = MyList. % _ is ignored </li></ul><ul><li>3> io:format(Var). % prints B </li></ul>© ThoughtWorks 2008
  6. 6. Functions as First Class Citizens <ul><li>1> Msg = &quot;I am scoped&quot;. </li></ul><ul><li>2> F = fun()-> </li></ul><ul><li>io:format(Msg) </li></ul><ul><li>end. </li></ul><ul><li>3> F(). % prints “ I am scoped ” </li></ul>© ThoughtWorks 2008
  7. 7. Tail Recursion <ul><li>Tail recursion is to ‘recursion’ as optimistic locking is to ‘locking’. </li></ul><ul><li>loop() -> </li></ul><ul><li>io:format( “ in constant space ” ), </li></ul><ul><li> loop() . </li></ul><ul><li>Automatic byte code manipulation </li></ul><ul><li>Last call optimization </li></ul>© ThoughtWorks 2008
  8. 8. Pop Quiz: Evaluation Strategies <ul><li>f( a(), b() ) </li></ul><ul><li>Order of evaluation </li></ul><ul><li>Lazy or strict? </li></ul><ul><li>Left, right, both, neither? </li></ul>© ThoughtWorks 2008
  9. 9. State <ul><li>Erlang avoids complex solutions in favor of simple ways to avoid the problem </li></ul><ul><li>State is immutable w/ single assignment </li></ul><ul><li>State is private, passed by value </li></ul><ul><li>Changing Perspectives </li></ul><ul><ul><li>Contention vs. Scale </li></ul></ul><ul><ul><li>Side Effects vs. Scale </li></ul></ul>© ThoughtWorks 2008
  10. 10. Erlang Process <ul><li>An Erlang process combines the best of </li></ul><ul><ul><li>An operating system process </li></ul></ul><ul><ul><li>An operating system thread </li></ul></ul><ul><ul><li>A “green thread” </li></ul></ul><ul><li>Controlled in user space by the ERTS </li></ul><ul><li>Costs less than 300 bytes </li></ul><ul><ul><li>Private heap </li></ul></ul><ul><ul><li>Private stack </li></ul></ul><ul><li>All processes are created equal </li></ul><ul><li>Incremental garbage collection </li></ul>© ThoughtWorks 2008
  11. 11. The Actor Model <ul><li>Asynchronous Message passing </li></ul><ul><li>Messages passed by value, not reference </li></ul><ul><li>One to one relationship </li></ul><ul><ul><li>An Actor </li></ul></ul><ul><ul><li>A mailbox </li></ul></ul><ul><ul><li>A Process </li></ul></ul><ul><ul><li>A Process ID, or pid </li></ul></ul>© ThoughtWorks 2008
  12. 12. Concurrency Primitives: spawn <ul><li>A built in function used to create a process. </li></ul><ul><li>Pid = spawn(Node, Module, Function, Args) </li></ul>© ThoughtWorks 2008
  13. 13. Another “Unbiased” Comparison <ul><li>In Erlang: </li></ul><ul><li>Pid = spawn(fun() -> io:format(“hello world”) end). </li></ul><ul><li>In Java: </li></ul><ul><li>Thread thread = new Thread() { </li></ul><ul><li>public void run() { </li></ul><ul><li>System.out.println(&quot;Hello World&quot;); </li></ul><ul><li>} </li></ul><ul><li>}.start(); </li></ul>© ThoughtWorks 2008
  14. 14. Concurrency Primitives: send <ul><li>! </li></ul>© ThoughtWorks 2008
  15. 15. Concurrency Primitives: send <ul><li>The send operator passes a message to a process. It is “fire and forget”. </li></ul><ul><li>Pid ! Msg </li></ul>© ThoughtWorks 2008
  16. 16. Concurrency Primitives: receive <ul><li>receive </li></ul><ul><li>Pattern1 [ when Guard1 ] -> </li></ul><ul><li>dosomething(). </li></ul><ul><li>Pattern2 [ when Guard2 ] -> </li></ul><ul><li>dosomethingelse(). </li></ul><ul><li>end </li></ul>© ThoughtWorks 2008
  17. 17. Reliability <ul><li>catch </li></ul><ul><li>try </li></ul><ul><li>Monitor </li></ul><ul><ul><li>monitor_node(Node, Bool) </li></ul></ul><ul><ul><li>Unidirectional </li></ul></ul><ul><li>Link </li></ul><ul><ul><li>Pid = spawn_link(Node, Module, Fun, Args) </li></ul></ul><ul><ul><li>Bi-directional </li></ul></ul>© ThoughtWorks 2008
  18. 18. The Opposite of Ruby © ThoughtWorks 2008 Erlang Ruby Orientation Functional Object Syntax Ugly Beautiful Concurrent Good Bad Track Record Back end Front end Execution Model Compiled Interpreted
  19. 19. Erlang Servers and Frameworks <ul><li>Mnesia – distributed database </li></ul><ul><li>YAWS – “yet another web server” </li></ul><ul><li>ErlyWeb – MVC web framework </li></ul><ul><li>EUnit </li></ul><ul><li>Jungerl – third party libraries </li></ul><ul><li>Perferl ( http://code.google.com/p/perferl ) </li></ul>© ThoughtWorks 2008
  20. 20. The Erlang Programming Language <ul><li>Dennis Byrne - ThoughtWorks </li></ul><ul><li>[email_address] </li></ul><ul><li>http://notdennisbyrne.blogspot.com/ </li></ul>© ThoughtWorks 2008

×