SlideShare a Scribd company logo
Three sessions about Erlang




         Session 2

       Mohamed Samy
       February 2010
Boolean value is Erlang
  
      The atoms true and false represent truth and
      falsehood.
  
      Equality testing (not matching) is via the ==
      operator. Inequality is via /=
  
      We also have =:= and =/= for exact equality
      and exact inequality (e.g 1.0 == 1 is true but
      1.0 =:= 1 is false).
  
      We have the boolean operators and, or, not,
      xor, andalso, orelse (the last two are for short-
      circuit evaluation).
The if expression
  if
       guardSeq1 ->
             body1
       ;
       ...
       guardSeqN ->
             bodyN
  end
  
       if...end is an expression, so it evaluates to a
       value.
The if expression
max(A, B) →
    if
       A>=B → A
       ;
       true → B
    end.


    The value of the whole if...end is the return value of the function.

    Each guard expression will be tested in order until one of them is
    true, this will be the value of the if...end

    Since the atom true always represents the truth value, we can put it
    as the last guard sequence of if...end to work like an 'else'.

    Erlang also has a case...end statement that supports pattern
    matching.
Anonymous functions
 
     In Erlang (and other languages) the programmer can write expressions that
     evaluate to functions. Erlang calls them "fun expressions".
 
     The syntax is:
 fun
           (params1) optional_when_guards1 →
            body1
       ;
           ...
           (paramsN) optional_when_guardsN ->
                 BodyN
       end
 
     The second form, like normal functions, uses pattern matching (or when ...)
     to choose which body to execute.
 
     When using the second form, all parameter lists must have the same
     number of params.
Anonymous functions
 
     Example 1:
 F1 = fun(X, Y) x+y end.
 Result = F1(10, 20).
 
     Example 2:
 F2 = fun
        (Age) when Age<=12 → child;
        (Age) when Age<= 19 → teen;
        (Age) when Age <= 50 → normal;
        ( _ ) → old
        end.
 F2(30).
Higher order functions
  
      These are functions that take functions as
      parameters or return functions as results.
  
      Some built-in HOFs:
      
          lists:any(testFunc, list)
           Even = fun (X)
                      (X rem 2) == 0
                    end.
           lists:any(Even,[1, 2 ,3, 4]).

           −   This will test if any member of the given list even (which
               is true).
      
          Also lists:all, lists:foldl, lists:foldr, lists:mapfoldl
  
      And many other functions!
Actors
 
     Concurrency has many hard-to-debug problems.
 
     For example, the lost update problem:
      void a( )
      {
          x= readSharedValue()
          x= x + 100
          writeSharedValue(X)
      }
      void b( )
      {
          x= readSharedValue()
          x= x + 50
          writeSharedValue(X)
      }
 
     Traditional languages solves this problems via locks
 
     But locks have problems of their own (e.g the deadlock problem).
 
     And there are many more problems...
Actors
 
     An actor is a computational entity which has a
     mailbox and behavior.
 
     Actors can send messages to each other;
     messages sent to an actor are buffered in its
     mailbox.
 
     Upon receiving a message an actor 's behavior
     is executed, now it can:
     
         Send messages to other actors;
     
         Create new actors;
     
         Designate new behavior for the next message it
         receives.
 
     There is no assumed sequence to the above
     actions and they could be carried out in parallel.
Actors vs. function calls
  
      A function A that calls B will (normally) wait for
      B to return, but sending message is inherently
      asynchronous.
  
      A called function must return to its caller, but an
      actor that receives a message can...
      
          Respond to the caller
      
          Respond by sending a message to another actor
          that it knows.
      
          Respond by sending to an actor specified in the
          message.
      
          Not send anything
      
          ...etc ...etc
Actors in Erlang
  
      Actors in Erlang are called processes.
  
      Erlang is designed for massive concurrency,
      Erlang processes are:
      
          Light-weight (grow and shrink dynamically).
      
          Have small memory footprint
      
          Are fast to create and terminate
      
          Their scheduling overhead is low.
  
      A program can have thousands of concurrent
      processes running with no problem.
Actors in Erlang

  
      Processes have 4 main operations in Erlang:
      
          spawn(...) creates a new process and returns its
          PID.
      
          ! is the message send operator
      
          receive...end is used to specify how to respond to
          messages
      
          register(...) is used to give names to a process,
          other parts of the program can then access that
          process via the name.
Creating new processes
  
      spawn(Module, Function, [ arg1, arg2...] )
      
          Creates a new process which starts by running
          function with the specified arguments.
      
          The caller of spawn resumes working normally and
          doesn't wait for the process to end.
      
          Returns a data of type Process ID (PID), which is
          used to access the process.
      
          There exist a number of other spawn(...) BIFs, for
          example spawn/4 for spawning a process at
          another node.
Message send

    receiver ! message
    
        The receiver is an expression whose value is one of:
        −  A PID
         − A registered process name (using register(...) )
         − A tuple {Name, Node} , both atoms.
    
        The message is any Erlang term.
    
        Notes:
        −   Message sending is asynchronous.
        −   The message is guaranteed to eventually reach the
            recipient, provided that the recipient exists.
        −   Sending to a non-existent node doesn't produce an
            error.
        −   The value of the expression a ! b is the same as b
receiving messages
receive
    Pattern1 [when GuardSeq1] ->
          Body1;
    ...
    PatternN [when GuardSeqN] ->
          BodyN
end

    Receives messages sent with `!` to the current process's
    mailbox.

    The [when guard_sequence] part is optional.

    When receiving a message, it is matched with each of the
    patterns (and guard sequences). When the first match
    happens, it's body is executed.
receiving messages

    Messages do not have to be received in the order
    they were sent.

    If the mailbox is empty or has no valid messages;
    execution is suspended (possibly indefinitely) until a
    suitable message arrives.

    The return value of the body is the return value of
    the whole receive expression.

    A receive can have a timeout:
              receive
                ....
                ....
               after timeout_expr ->
                    Body
              end
register ( )

    register(Name, Pid)
    
        Name is an atom, used to access the process later.
    
        Pid is....a PID
    
        Remember, the receiver in a message send operation
        like a ! b can be a registered process name.
    
        It is allowed (but not required) to give the same name for
        a process and function.
Example actor: Hi, Bye!
-module(hi_bye).
-export(run/0).
run ( )->
 receive
    hi →
    io:format("bye!~n", [ ]),
    run( ),
  end.


after compiling the module:
erlang> PID = spawn(hi_bye, run, [ ]).
erlang> PID ! hi.
Example actor: a counter

(Interactive)
Ping! Pong!
We have 2 processes: player1 & player2
  
      player2 goes in a loop where it can receive one of 2 messages:
       −   {ping, ReplyToPID} makes player2 respond by sending the message
           pong to the process denoted by ReplyToPID
       −   stop makes player2 print “finish” on the screen and exit the loop
  
      player1 takes a parameter N and does the following:
       −   repeat N times:
             
               send the tuple {ping, self( )} to player2
             
               receive the atom pong from player2
       −   send stop to player2
       −   print "finish" and exit.
  
      player2 will have a name created using register( ), all messages
      to player2 will use the registered name.
  
      Note: self( ) is a built-in function that returns the ID of the
      process which called it.
Ping! Pong!
Ping! Pong!
Distributed Erlang
  
      Each Erlang system running on a computer is
      called an Erlang node.
  
      A node can have a name, either a short name
      or a long name.
  
      The name allows other nodes to access it.
  
      For two Erlang nodes to communicate, they
      must have the same secret cookie. (for security
      reasons).
Distributed Erlang
  
      Short and long names:
        −   When using short names we assume that all nodes are
            in the same IP domain and we can use only the first
            component of the IP address.
        −   If we want to use nodes in different domains we use
            -name instead for long names, but then all IP address
            must be given in full.
Names and cookies
 
     Names and cookies are passed to Erlang via
     the command line:
     erl -sname server -setcookie mysecret
     OR
     erl -name server@127.0.0.1 -setcookie mysecret
         −   Note: On Windows you can use erl.exe (console) or
             werl.exe (GUI).
     
         A running program can read its own node's name
         with the node() function and can set and get the
         cookie with the set_cookie(...) and get_cookie( )
         functions.
Distributed Erlang
  
      So how do we send a message to a process to
      another node?
  
      We already know!!!
  
      {Name, Node} ! msg
  
      Example:
      {my_process, 'server@samy-pc' } ! {connect}
Final example
 
     A small message board application:
     
         Server: The server will run in an infinite loop and
         can receive these messages:
         −   {connect Username} to add the user to its user list
         −   {say Username Text} to display a user message
         −   showusers to display the list of connected users.
     
         Client: The client will connect to a server with the
         {connect,...} message, and then keep sending text
         to the server via {say, ...}
Help: Erlang command line on Windows

 
     Go to start menu → Run
 
     Write: cmd.exe and press enter
 
     From the command line:
     c:> cd "c:Program fileserl5.7.4bin" <enter>
     c:...> werl.exe -sname node1 -setcookie mysecret
 
     Note: The name of the node will be shown in
     the Erlang command line:
Let's take a breath
More stuff in Erlang...
  
      Error handling using workers, supervisors
      and supervision trees.
  
      Bit syntax and bit operations
  
      The built-in Mnesia database (and others)
  
      List comprehensions:
      L = [ {X, Y} || X<- [1,2,3,4], Y <-[1,2,3,4], X+Y==5]
      L = [ {1, 4}, {2, 3}, {3, 2}, {4,1} ]
  
      Web applications, GUI applications,
      Networking, OpenGL, other databases...
  
      ....etc
The end!

More Related Content

What's hot

Phân tích mã độc cơ bản - báo cáo thực tập
Phân tích mã độc cơ bản - báo cáo thực tậpPhân tích mã độc cơ bản - báo cáo thực tập
Phân tích mã độc cơ bản - báo cáo thực tập
Phạm Trung Đức
 
VMware Cost Savings Through Virtualization
VMware Cost Savings Through VirtualizationVMware Cost Savings Through Virtualization
VMware Cost Savings Through Virtualization
hypknight
 
Hướng dẫn sử dụng hệ điều hành Ubuntu
Hướng dẫn sử dụng hệ điều hành UbuntuHướng dẫn sử dụng hệ điều hành Ubuntu
Hướng dẫn sử dụng hệ điều hành UbuntuQuang Ngoc
 
Bài 3 xử lý sự cố phần mềm văn phòng
Bài 3   xử lý sự cố phần mềm văn phòngBài 3   xử lý sự cố phần mềm văn phòng
Bài 3 xử lý sự cố phần mềm văn phòngMasterCode.vn
 
VMworld 2013: VMware vSphere Replication: Technical Walk-Through with Enginee...
VMworld 2013: VMware vSphere Replication: Technical Walk-Through with Enginee...VMworld 2013: VMware vSphere Replication: Technical Walk-Through with Enginee...
VMworld 2013: VMware vSphere Replication: Technical Walk-Through with Enginee...
VMworld
 
Bao cao thực tập Điện toán đám mây
Bao cao thực tập Điện toán đám mâyBao cao thực tập Điện toán đám mây
Bao cao thực tập Điện toán đám mây
Văn Ân Phạm
 
Active Directory 2019 v2.pptx
Active Directory 2019 v2.pptxActive Directory 2019 v2.pptx
Active Directory 2019 v2.pptx
Pradeep Kapkoti
 
Continuous Integration and DevOps with Open Build Service(OBS)
Continuous Integration and DevOps with Open Build Service(OBS)Continuous Integration and DevOps with Open Build Service(OBS)
Continuous Integration and DevOps with Open Build Service(OBS)
Ralf Dannert
 
XXE injection - Nguyễn Tăng Hưng
XXE injection - Nguyễn Tăng HưngXXE injection - Nguyễn Tăng Hưng
XXE injection - Nguyễn Tăng Hưng
Võ Thái Lâm
 
Power vc for powervm deep dive tips &amp; tricks
Power vc for powervm deep dive tips &amp; tricksPower vc for powervm deep dive tips &amp; tricks
Power vc for powervm deep dive tips &amp; tricks
solarisyougood
 
Triển khai wsus windows server update services
Triển khai wsus  windows server update servicesTriển khai wsus  windows server update services
Triển khai wsus windows server update services
laonap166
 
Dự Đoán Lỗ Hổng Phần Mềm Dựa Trên Kỹ Thuật Khai Phá Dữ Liệu _08300812092019
Dự Đoán Lỗ Hổng Phần Mềm Dựa Trên Kỹ Thuật Khai Phá Dữ Liệu _08300812092019Dự Đoán Lỗ Hổng Phần Mềm Dựa Trên Kỹ Thuật Khai Phá Dữ Liệu _08300812092019
Dự Đoán Lỗ Hổng Phần Mềm Dựa Trên Kỹ Thuật Khai Phá Dữ Liệu _08300812092019
hanhha12
 
Move Your Desktops and Applications to AWS with Amazon WorkSpaces and AppStre...
Move Your Desktops and Applications to AWS with Amazon WorkSpaces and AppStre...Move Your Desktops and Applications to AWS with Amazon WorkSpaces and AppStre...
Move Your Desktops and Applications to AWS with Amazon WorkSpaces and AppStre...
Amazon Web Services
 
AWS basics
AWS basicsAWS basics
AWS basics
mbaric
 
Mô hình điện toán đám mây
Mô hình điện toán đám mâyMô hình điện toán đám mây
Mô hình điện toán đám mây
PhamTuanKhiem
 
AWSome Day Online Conference 2019 - Module 2 AWS Core Services.pdf
AWSome Day Online Conference 2019 - Module 2 AWS Core Services.pdfAWSome Day Online Conference 2019 - Module 2 AWS Core Services.pdf
AWSome Day Online Conference 2019 - Module 2 AWS Core Services.pdf
Amazon Web Services
 
Unidad 10 - Puesta en marcha del sistema
Unidad 10 - Puesta en marcha del sistemaUnidad 10 - Puesta en marcha del sistema
Unidad 10 - Puesta en marcha del sistema
vverdu
 
Quick-Start Guide: Deploying Your Cloudian HyperStore Hybrid Storage Service
Quick-Start Guide: Deploying Your Cloudian HyperStore Hybrid Storage ServiceQuick-Start Guide: Deploying Your Cloudian HyperStore Hybrid Storage Service
Quick-Start Guide: Deploying Your Cloudian HyperStore Hybrid Storage Service
Cloudian
 
Amazon ec2
Amazon ec2Amazon ec2
Amazon ec2
Joydip Ghosh
 

What's hot (20)

Phân tích mã độc cơ bản - báo cáo thực tập
Phân tích mã độc cơ bản - báo cáo thực tậpPhân tích mã độc cơ bản - báo cáo thực tập
Phân tích mã độc cơ bản - báo cáo thực tập
 
VMware Cost Savings Through Virtualization
VMware Cost Savings Through VirtualizationVMware Cost Savings Through Virtualization
VMware Cost Savings Through Virtualization
 
Hướng dẫn sử dụng hệ điều hành Ubuntu
Hướng dẫn sử dụng hệ điều hành UbuntuHướng dẫn sử dụng hệ điều hành Ubuntu
Hướng dẫn sử dụng hệ điều hành Ubuntu
 
Bài 3 xử lý sự cố phần mềm văn phòng
Bài 3   xử lý sự cố phần mềm văn phòngBài 3   xử lý sự cố phần mềm văn phòng
Bài 3 xử lý sự cố phần mềm văn phòng
 
VMworld 2013: VMware vSphere Replication: Technical Walk-Through with Enginee...
VMworld 2013: VMware vSphere Replication: Technical Walk-Through with Enginee...VMworld 2013: VMware vSphere Replication: Technical Walk-Through with Enginee...
VMworld 2013: VMware vSphere Replication: Technical Walk-Through with Enginee...
 
Bao cao thực tập Điện toán đám mây
Bao cao thực tập Điện toán đám mâyBao cao thực tập Điện toán đám mây
Bao cao thực tập Điện toán đám mây
 
Active Directory 2019 v2.pptx
Active Directory 2019 v2.pptxActive Directory 2019 v2.pptx
Active Directory 2019 v2.pptx
 
Continuous Integration and DevOps with Open Build Service(OBS)
Continuous Integration and DevOps with Open Build Service(OBS)Continuous Integration and DevOps with Open Build Service(OBS)
Continuous Integration and DevOps with Open Build Service(OBS)
 
XXE injection - Nguyễn Tăng Hưng
XXE injection - Nguyễn Tăng HưngXXE injection - Nguyễn Tăng Hưng
XXE injection - Nguyễn Tăng Hưng
 
Power vc for powervm deep dive tips &amp; tricks
Power vc for powervm deep dive tips &amp; tricksPower vc for powervm deep dive tips &amp; tricks
Power vc for powervm deep dive tips &amp; tricks
 
Triển khai wsus windows server update services
Triển khai wsus  windows server update servicesTriển khai wsus  windows server update services
Triển khai wsus windows server update services
 
Dự Đoán Lỗ Hổng Phần Mềm Dựa Trên Kỹ Thuật Khai Phá Dữ Liệu _08300812092019
Dự Đoán Lỗ Hổng Phần Mềm Dựa Trên Kỹ Thuật Khai Phá Dữ Liệu _08300812092019Dự Đoán Lỗ Hổng Phần Mềm Dựa Trên Kỹ Thuật Khai Phá Dữ Liệu _08300812092019
Dự Đoán Lỗ Hổng Phần Mềm Dựa Trên Kỹ Thuật Khai Phá Dữ Liệu _08300812092019
 
Move Your Desktops and Applications to AWS with Amazon WorkSpaces and AppStre...
Move Your Desktops and Applications to AWS with Amazon WorkSpaces and AppStre...Move Your Desktops and Applications to AWS with Amazon WorkSpaces and AppStre...
Move Your Desktops and Applications to AWS with Amazon WorkSpaces and AppStre...
 
La virtualisation
La virtualisationLa virtualisation
La virtualisation
 
AWS basics
AWS basicsAWS basics
AWS basics
 
Mô hình điện toán đám mây
Mô hình điện toán đám mâyMô hình điện toán đám mây
Mô hình điện toán đám mây
 
AWSome Day Online Conference 2019 - Module 2 AWS Core Services.pdf
AWSome Day Online Conference 2019 - Module 2 AWS Core Services.pdfAWSome Day Online Conference 2019 - Module 2 AWS Core Services.pdf
AWSome Day Online Conference 2019 - Module 2 AWS Core Services.pdf
 
Unidad 10 - Puesta en marcha del sistema
Unidad 10 - Puesta en marcha del sistemaUnidad 10 - Puesta en marcha del sistema
Unidad 10 - Puesta en marcha del sistema
 
Quick-Start Guide: Deploying Your Cloudian HyperStore Hybrid Storage Service
Quick-Start Guide: Deploying Your Cloudian HyperStore Hybrid Storage ServiceQuick-Start Guide: Deploying Your Cloudian HyperStore Hybrid Storage Service
Quick-Start Guide: Deploying Your Cloudian HyperStore Hybrid Storage Service
 
Amazon ec2
Amazon ec2Amazon ec2
Amazon ec2
 

Viewers also liked

Computational thinking in Egypt
Computational thinking in EgyptComputational thinking in Egypt
Computational thinking in Egypt
mohamedsamyali
 
C# Summer course - Lecture 3
C# Summer course - Lecture 3C# Summer course - Lecture 3
C# Summer course - Lecture 3
mohamedsamyali
 
Erlang session1
Erlang session1Erlang session1
Erlang session1
mohamedsamyali
 
C# Summer course - Lecture 4
C# Summer course - Lecture 4C# Summer course - Lecture 4
C# Summer course - Lecture 4
mohamedsamyali
 
Presentation skills for Graduation projects
Presentation skills for Graduation projectsPresentation skills for Graduation projects
Presentation skills for Graduation projects
mohamedsamyali
 
Smalltalk, the dynamic language
Smalltalk, the dynamic languageSmalltalk, the dynamic language
Smalltalk, the dynamic language
mohamedsamyali
 
Themes for graduation projects 2010
Themes for graduation projects   2010Themes for graduation projects   2010
Themes for graduation projects 2010
mohamedsamyali
 

Viewers also liked (8)

Spray intro
Spray introSpray intro
Spray intro
 
Computational thinking in Egypt
Computational thinking in EgyptComputational thinking in Egypt
Computational thinking in Egypt
 
C# Summer course - Lecture 3
C# Summer course - Lecture 3C# Summer course - Lecture 3
C# Summer course - Lecture 3
 
Erlang session1
Erlang session1Erlang session1
Erlang session1
 
C# Summer course - Lecture 4
C# Summer course - Lecture 4C# Summer course - Lecture 4
C# Summer course - Lecture 4
 
Presentation skills for Graduation projects
Presentation skills for Graduation projectsPresentation skills for Graduation projects
Presentation skills for Graduation projects
 
Smalltalk, the dynamic language
Smalltalk, the dynamic languageSmalltalk, the dynamic language
Smalltalk, the dynamic language
 
Themes for graduation projects 2010
Themes for graduation projects   2010Themes for graduation projects   2010
Themes for graduation projects 2010
 

Similar to Erlang session2

Erlang Message Passing Concurrency, For The Win
Erlang  Message  Passing  Concurrency,  For  The  WinErlang  Message  Passing  Concurrency,  For  The  Win
Erlang Message Passing Concurrency, For The Win
l xf
 
Javascript
JavascriptJavascript
Javascript
Gita Kriz
 
Concurrency, Robustness & Elixir SoCraTes 2015
Concurrency, Robustness & Elixir SoCraTes 2015Concurrency, Robustness & Elixir SoCraTes 2015
Concurrency, Robustness & Elixir SoCraTes 2015
steffenbauer
 
The Erlang Programming Language
The Erlang Programming LanguageThe Erlang Programming Language
The Erlang Programming Language
Dennis Byrne
 
Linux basics
Linux basicsLinux basics
Linux basics
sirmanohar
 
02 fundamentals
02 fundamentals02 fundamentals
02 fundamentals
sirmanohar
 
Introduction to python programming ( part-2 )
Introduction to python programming ( part-2 )Introduction to python programming ( part-2 )
Introduction to python programming ( part-2 )
Ziyauddin Shaik
 
Erlang, an overview
Erlang, an overviewErlang, an overview
Erlang, an overview
Patrick Huesler
 
Erlang bootstrap course
Erlang bootstrap courseErlang bootstrap course
Erlang bootstrap course
Martin Logan
 
Python Session - 4
Python Session - 4Python Session - 4
Python Session - 4
AnirudhaGaikwad4
 
Time and Space Complexity Analysis.pptx
Time and Space Complexity Analysis.pptxTime and Space Complexity Analysis.pptx
Time and Space Complexity Analysis.pptx
dudelover
 
Introduction To Erlang Final
Introduction To Erlang   FinalIntroduction To Erlang   Final
Introduction To Erlang Final
SinarShebl
 
Phyton Learning extracts
Phyton Learning extracts Phyton Learning extracts
Phyton Learning extracts
Pavan Babu .G
 
ISTA 130 Lab 21 Turtle ReviewHere are all of the turt.docx
ISTA 130 Lab 21 Turtle ReviewHere are all of the turt.docxISTA 130 Lab 21 Turtle ReviewHere are all of the turt.docx
ISTA 130 Lab 21 Turtle ReviewHere are all of the turt.docx
priestmanmable
 
Introduction To Rust part II Presentation
Introduction To Rust part II PresentationIntroduction To Rust part II Presentation
Introduction To Rust part II Presentation
Knoldus Inc.
 
Introduction To Rust part II Presentation
Introduction To Rust part II PresentationIntroduction To Rust part II Presentation
Introduction To Rust part II Presentation
Knoldus Inc.
 
2 Functions2.pptx
2 Functions2.pptx2 Functions2.pptx
2 Functions2.pptx
RohitYadav830391
 
02 functions, variables, basic input and output of c++
02   functions, variables, basic input and output of c++02   functions, variables, basic input and output of c++
02 functions, variables, basic input and output of c++
Manzoor ALam
 
Functions in python
Functions in pythonFunctions in python
Functions in python
Santosh Verma
 
Licão 13 functions
Licão 13 functionsLicão 13 functions
Licão 13 functions
Acácio Oliveira
 

Similar to Erlang session2 (20)

Erlang Message Passing Concurrency, For The Win
Erlang  Message  Passing  Concurrency,  For  The  WinErlang  Message  Passing  Concurrency,  For  The  Win
Erlang Message Passing Concurrency, For The Win
 
Javascript
JavascriptJavascript
Javascript
 
Concurrency, Robustness & Elixir SoCraTes 2015
Concurrency, Robustness & Elixir SoCraTes 2015Concurrency, Robustness & Elixir SoCraTes 2015
Concurrency, Robustness & Elixir SoCraTes 2015
 
The Erlang Programming Language
The Erlang Programming LanguageThe Erlang Programming Language
The Erlang Programming Language
 
Linux basics
Linux basicsLinux basics
Linux basics
 
02 fundamentals
02 fundamentals02 fundamentals
02 fundamentals
 
Introduction to python programming ( part-2 )
Introduction to python programming ( part-2 )Introduction to python programming ( part-2 )
Introduction to python programming ( part-2 )
 
Erlang, an overview
Erlang, an overviewErlang, an overview
Erlang, an overview
 
Erlang bootstrap course
Erlang bootstrap courseErlang bootstrap course
Erlang bootstrap course
 
Python Session - 4
Python Session - 4Python Session - 4
Python Session - 4
 
Time and Space Complexity Analysis.pptx
Time and Space Complexity Analysis.pptxTime and Space Complexity Analysis.pptx
Time and Space Complexity Analysis.pptx
 
Introduction To Erlang Final
Introduction To Erlang   FinalIntroduction To Erlang   Final
Introduction To Erlang Final
 
Phyton Learning extracts
Phyton Learning extracts Phyton Learning extracts
Phyton Learning extracts
 
ISTA 130 Lab 21 Turtle ReviewHere are all of the turt.docx
ISTA 130 Lab 21 Turtle ReviewHere are all of the turt.docxISTA 130 Lab 21 Turtle ReviewHere are all of the turt.docx
ISTA 130 Lab 21 Turtle ReviewHere are all of the turt.docx
 
Introduction To Rust part II Presentation
Introduction To Rust part II PresentationIntroduction To Rust part II Presentation
Introduction To Rust part II Presentation
 
Introduction To Rust part II Presentation
Introduction To Rust part II PresentationIntroduction To Rust part II Presentation
Introduction To Rust part II Presentation
 
2 Functions2.pptx
2 Functions2.pptx2 Functions2.pptx
2 Functions2.pptx
 
02 functions, variables, basic input and output of c++
02   functions, variables, basic input and output of c++02   functions, variables, basic input and output of c++
02 functions, variables, basic input and output of c++
 
Functions in python
Functions in pythonFunctions in python
Functions in python
 
Licão 13 functions
Licão 13 functionsLicão 13 functions
Licão 13 functions
 

Recently uploaded

Letter and Document Automation for Bonterra Impact Management (fka Social Sol...
Letter and Document Automation for Bonterra Impact Management (fka Social Sol...Letter and Document Automation for Bonterra Impact Management (fka Social Sol...
Letter and Document Automation for Bonterra Impact Management (fka Social Sol...
Jeffrey Haguewood
 
Overcoming the PLG Trap: Lessons from Canva's Head of Sales & Head of EMEA Da...
Overcoming the PLG Trap: Lessons from Canva's Head of Sales & Head of EMEA Da...Overcoming the PLG Trap: Lessons from Canva's Head of Sales & Head of EMEA Da...
Overcoming the PLG Trap: Lessons from Canva's Head of Sales & Head of EMEA Da...
saastr
 
GraphRAG for Life Science to increase LLM accuracy
GraphRAG for Life Science to increase LLM accuracyGraphRAG for Life Science to increase LLM accuracy
GraphRAG for Life Science to increase LLM accuracy
Tomaz Bratanic
 
Best 20 SEO Techniques To Improve Website Visibility In SERP
Best 20 SEO Techniques To Improve Website Visibility In SERPBest 20 SEO Techniques To Improve Website Visibility In SERP
Best 20 SEO Techniques To Improve Website Visibility In SERP
Pixlogix Infotech
 
Operating System Used by Users in day-to-day life.pptx
Operating System Used by Users in day-to-day life.pptxOperating System Used by Users in day-to-day life.pptx
Operating System Used by Users in day-to-day life.pptx
Pravash Chandra Das
 
Unlock the Future of Search with MongoDB Atlas_ Vector Search Unleashed.pdf
Unlock the Future of Search with MongoDB Atlas_ Vector Search Unleashed.pdfUnlock the Future of Search with MongoDB Atlas_ Vector Search Unleashed.pdf
Unlock the Future of Search with MongoDB Atlas_ Vector Search Unleashed.pdf
Malak Abu Hammad
 
GenAI Pilot Implementation in the organizations
GenAI Pilot Implementation in the organizationsGenAI Pilot Implementation in the organizations
GenAI Pilot Implementation in the organizations
kumardaparthi1024
 
Let's Integrate MuleSoft RPA, COMPOSER, APM with AWS IDP along with Slack
Let's Integrate MuleSoft RPA, COMPOSER, APM with AWS IDP along with SlackLet's Integrate MuleSoft RPA, COMPOSER, APM with AWS IDP along with Slack
Let's Integrate MuleSoft RPA, COMPOSER, APM with AWS IDP along with Slack
shyamraj55
 
Deep Dive: Getting Funded with Jason Jason Lemkin Founder & CEO @ SaaStr
Deep Dive: Getting Funded with Jason Jason Lemkin Founder & CEO @ SaaStrDeep Dive: Getting Funded with Jason Jason Lemkin Founder & CEO @ SaaStr
Deep Dive: Getting Funded with Jason Jason Lemkin Founder & CEO @ SaaStr
saastr
 
Generating privacy-protected synthetic data using Secludy and Milvus
Generating privacy-protected synthetic data using Secludy and MilvusGenerating privacy-protected synthetic data using Secludy and Milvus
Generating privacy-protected synthetic data using Secludy and Milvus
Zilliz
 
leewayhertz.com-AI in predictive maintenance Use cases technologies benefits ...
leewayhertz.com-AI in predictive maintenance Use cases technologies benefits ...leewayhertz.com-AI in predictive maintenance Use cases technologies benefits ...
leewayhertz.com-AI in predictive maintenance Use cases technologies benefits ...
alexjohnson7307
 
Artificial Intelligence for XMLDevelopment
Artificial Intelligence for XMLDevelopmentArtificial Intelligence for XMLDevelopment
Artificial Intelligence for XMLDevelopment
Octavian Nadolu
 
Ocean lotus Threat actors project by John Sitima 2024 (1).pptx
Ocean lotus Threat actors project by John Sitima 2024 (1).pptxOcean lotus Threat actors project by John Sitima 2024 (1).pptx
Ocean lotus Threat actors project by John Sitima 2024 (1).pptx
SitimaJohn
 
Energy Efficient Video Encoding for Cloud and Edge Computing Instances
Energy Efficient Video Encoding for Cloud and Edge Computing InstancesEnergy Efficient Video Encoding for Cloud and Edge Computing Instances
Energy Efficient Video Encoding for Cloud and Edge Computing Instances
Alpen-Adria-Universität
 
Azure API Management to expose backend services securely
Azure API Management to expose backend services securelyAzure API Management to expose backend services securely
Azure API Management to expose backend services securely
Dinusha Kumarasiri
 
Skybuffer SAM4U tool for SAP license adoption
Skybuffer SAM4U tool for SAP license adoptionSkybuffer SAM4U tool for SAP license adoption
Skybuffer SAM4U tool for SAP license adoption
Tatiana Kojar
 
Serial Arm Control in Real Time Presentation
Serial Arm Control in Real Time PresentationSerial Arm Control in Real Time Presentation
Serial Arm Control in Real Time Presentation
tolgahangng
 
Main news related to the CCS TSI 2023 (2023/1695)
Main news related to the CCS TSI 2023 (2023/1695)Main news related to the CCS TSI 2023 (2023/1695)
Main news related to the CCS TSI 2023 (2023/1695)
Jakub Marek
 
Columbus Data & Analytics Wednesdays - June 2024
Columbus Data & Analytics Wednesdays - June 2024Columbus Data & Analytics Wednesdays - June 2024
Columbus Data & Analytics Wednesdays - June 2024
Jason Packer
 
June Patch Tuesday
June Patch TuesdayJune Patch Tuesday
June Patch Tuesday
Ivanti
 

Recently uploaded (20)

Letter and Document Automation for Bonterra Impact Management (fka Social Sol...
Letter and Document Automation for Bonterra Impact Management (fka Social Sol...Letter and Document Automation for Bonterra Impact Management (fka Social Sol...
Letter and Document Automation for Bonterra Impact Management (fka Social Sol...
 
Overcoming the PLG Trap: Lessons from Canva's Head of Sales & Head of EMEA Da...
Overcoming the PLG Trap: Lessons from Canva's Head of Sales & Head of EMEA Da...Overcoming the PLG Trap: Lessons from Canva's Head of Sales & Head of EMEA Da...
Overcoming the PLG Trap: Lessons from Canva's Head of Sales & Head of EMEA Da...
 
GraphRAG for Life Science to increase LLM accuracy
GraphRAG for Life Science to increase LLM accuracyGraphRAG for Life Science to increase LLM accuracy
GraphRAG for Life Science to increase LLM accuracy
 
Best 20 SEO Techniques To Improve Website Visibility In SERP
Best 20 SEO Techniques To Improve Website Visibility In SERPBest 20 SEO Techniques To Improve Website Visibility In SERP
Best 20 SEO Techniques To Improve Website Visibility In SERP
 
Operating System Used by Users in day-to-day life.pptx
Operating System Used by Users in day-to-day life.pptxOperating System Used by Users in day-to-day life.pptx
Operating System Used by Users in day-to-day life.pptx
 
Unlock the Future of Search with MongoDB Atlas_ Vector Search Unleashed.pdf
Unlock the Future of Search with MongoDB Atlas_ Vector Search Unleashed.pdfUnlock the Future of Search with MongoDB Atlas_ Vector Search Unleashed.pdf
Unlock the Future of Search with MongoDB Atlas_ Vector Search Unleashed.pdf
 
GenAI Pilot Implementation in the organizations
GenAI Pilot Implementation in the organizationsGenAI Pilot Implementation in the organizations
GenAI Pilot Implementation in the organizations
 
Let's Integrate MuleSoft RPA, COMPOSER, APM with AWS IDP along with Slack
Let's Integrate MuleSoft RPA, COMPOSER, APM with AWS IDP along with SlackLet's Integrate MuleSoft RPA, COMPOSER, APM with AWS IDP along with Slack
Let's Integrate MuleSoft RPA, COMPOSER, APM with AWS IDP along with Slack
 
Deep Dive: Getting Funded with Jason Jason Lemkin Founder & CEO @ SaaStr
Deep Dive: Getting Funded with Jason Jason Lemkin Founder & CEO @ SaaStrDeep Dive: Getting Funded with Jason Jason Lemkin Founder & CEO @ SaaStr
Deep Dive: Getting Funded with Jason Jason Lemkin Founder & CEO @ SaaStr
 
Generating privacy-protected synthetic data using Secludy and Milvus
Generating privacy-protected synthetic data using Secludy and MilvusGenerating privacy-protected synthetic data using Secludy and Milvus
Generating privacy-protected synthetic data using Secludy and Milvus
 
leewayhertz.com-AI in predictive maintenance Use cases technologies benefits ...
leewayhertz.com-AI in predictive maintenance Use cases technologies benefits ...leewayhertz.com-AI in predictive maintenance Use cases technologies benefits ...
leewayhertz.com-AI in predictive maintenance Use cases technologies benefits ...
 
Artificial Intelligence for XMLDevelopment
Artificial Intelligence for XMLDevelopmentArtificial Intelligence for XMLDevelopment
Artificial Intelligence for XMLDevelopment
 
Ocean lotus Threat actors project by John Sitima 2024 (1).pptx
Ocean lotus Threat actors project by John Sitima 2024 (1).pptxOcean lotus Threat actors project by John Sitima 2024 (1).pptx
Ocean lotus Threat actors project by John Sitima 2024 (1).pptx
 
Energy Efficient Video Encoding for Cloud and Edge Computing Instances
Energy Efficient Video Encoding for Cloud and Edge Computing InstancesEnergy Efficient Video Encoding for Cloud and Edge Computing Instances
Energy Efficient Video Encoding for Cloud and Edge Computing Instances
 
Azure API Management to expose backend services securely
Azure API Management to expose backend services securelyAzure API Management to expose backend services securely
Azure API Management to expose backend services securely
 
Skybuffer SAM4U tool for SAP license adoption
Skybuffer SAM4U tool for SAP license adoptionSkybuffer SAM4U tool for SAP license adoption
Skybuffer SAM4U tool for SAP license adoption
 
Serial Arm Control in Real Time Presentation
Serial Arm Control in Real Time PresentationSerial Arm Control in Real Time Presentation
Serial Arm Control in Real Time Presentation
 
Main news related to the CCS TSI 2023 (2023/1695)
Main news related to the CCS TSI 2023 (2023/1695)Main news related to the CCS TSI 2023 (2023/1695)
Main news related to the CCS TSI 2023 (2023/1695)
 
Columbus Data & Analytics Wednesdays - June 2024
Columbus Data & Analytics Wednesdays - June 2024Columbus Data & Analytics Wednesdays - June 2024
Columbus Data & Analytics Wednesdays - June 2024
 
June Patch Tuesday
June Patch TuesdayJune Patch Tuesday
June Patch Tuesday
 

Erlang session2

  • 1. Three sessions about Erlang Session 2 Mohamed Samy February 2010
  • 2. Boolean value is Erlang  The atoms true and false represent truth and falsehood.  Equality testing (not matching) is via the == operator. Inequality is via /=  We also have =:= and =/= for exact equality and exact inequality (e.g 1.0 == 1 is true but 1.0 =:= 1 is false).  We have the boolean operators and, or, not, xor, andalso, orelse (the last two are for short- circuit evaluation).
  • 3. The if expression if guardSeq1 -> body1 ; ... guardSeqN -> bodyN end  if...end is an expression, so it evaluates to a value.
  • 4. The if expression max(A, B) → if A>=B → A ; true → B end.  The value of the whole if...end is the return value of the function.  Each guard expression will be tested in order until one of them is true, this will be the value of the if...end  Since the atom true always represents the truth value, we can put it as the last guard sequence of if...end to work like an 'else'.  Erlang also has a case...end statement that supports pattern matching.
  • 5. Anonymous functions  In Erlang (and other languages) the programmer can write expressions that evaluate to functions. Erlang calls them "fun expressions".  The syntax is: fun (params1) optional_when_guards1 → body1 ; ... (paramsN) optional_when_guardsN -> BodyN end  The second form, like normal functions, uses pattern matching (or when ...) to choose which body to execute.  When using the second form, all parameter lists must have the same number of params.
  • 6. Anonymous functions  Example 1: F1 = fun(X, Y) x+y end. Result = F1(10, 20).  Example 2: F2 = fun (Age) when Age<=12 → child; (Age) when Age<= 19 → teen; (Age) when Age <= 50 → normal; ( _ ) → old end. F2(30).
  • 7. Higher order functions  These are functions that take functions as parameters or return functions as results.  Some built-in HOFs:  lists:any(testFunc, list) Even = fun (X) (X rem 2) == 0 end. lists:any(Even,[1, 2 ,3, 4]). − This will test if any member of the given list even (which is true).  Also lists:all, lists:foldl, lists:foldr, lists:mapfoldl  And many other functions!
  • 8. Actors  Concurrency has many hard-to-debug problems.  For example, the lost update problem: void a( ) { x= readSharedValue() x= x + 100 writeSharedValue(X) } void b( ) { x= readSharedValue() x= x + 50 writeSharedValue(X) }  Traditional languages solves this problems via locks  But locks have problems of their own (e.g the deadlock problem).  And there are many more problems...
  • 9. Actors  An actor is a computational entity which has a mailbox and behavior.  Actors can send messages to each other; messages sent to an actor are buffered in its mailbox.  Upon receiving a message an actor 's behavior is executed, now it can:  Send messages to other actors;  Create new actors;  Designate new behavior for the next message it receives.  There is no assumed sequence to the above actions and they could be carried out in parallel.
  • 10. Actors vs. function calls  A function A that calls B will (normally) wait for B to return, but sending message is inherently asynchronous.  A called function must return to its caller, but an actor that receives a message can...  Respond to the caller  Respond by sending a message to another actor that it knows.  Respond by sending to an actor specified in the message.  Not send anything  ...etc ...etc
  • 11. Actors in Erlang  Actors in Erlang are called processes.  Erlang is designed for massive concurrency, Erlang processes are:  Light-weight (grow and shrink dynamically).  Have small memory footprint  Are fast to create and terminate  Their scheduling overhead is low.  A program can have thousands of concurrent processes running with no problem.
  • 12. Actors in Erlang  Processes have 4 main operations in Erlang:  spawn(...) creates a new process and returns its PID.  ! is the message send operator  receive...end is used to specify how to respond to messages  register(...) is used to give names to a process, other parts of the program can then access that process via the name.
  • 13. Creating new processes  spawn(Module, Function, [ arg1, arg2...] )  Creates a new process which starts by running function with the specified arguments.  The caller of spawn resumes working normally and doesn't wait for the process to end.  Returns a data of type Process ID (PID), which is used to access the process.  There exist a number of other spawn(...) BIFs, for example spawn/4 for spawning a process at another node.
  • 14. Message send  receiver ! message  The receiver is an expression whose value is one of: − A PID − A registered process name (using register(...) ) − A tuple {Name, Node} , both atoms.  The message is any Erlang term.  Notes: − Message sending is asynchronous. − The message is guaranteed to eventually reach the recipient, provided that the recipient exists. − Sending to a non-existent node doesn't produce an error. − The value of the expression a ! b is the same as b
  • 15. receiving messages receive Pattern1 [when GuardSeq1] -> Body1; ... PatternN [when GuardSeqN] -> BodyN end  Receives messages sent with `!` to the current process's mailbox.  The [when guard_sequence] part is optional.  When receiving a message, it is matched with each of the patterns (and guard sequences). When the first match happens, it's body is executed.
  • 16. receiving messages  Messages do not have to be received in the order they were sent.  If the mailbox is empty or has no valid messages; execution is suspended (possibly indefinitely) until a suitable message arrives.  The return value of the body is the return value of the whole receive expression.  A receive can have a timeout: receive .... .... after timeout_expr -> Body end
  • 17. register ( )  register(Name, Pid)  Name is an atom, used to access the process later.  Pid is....a PID  Remember, the receiver in a message send operation like a ! b can be a registered process name.  It is allowed (but not required) to give the same name for a process and function.
  • 18. Example actor: Hi, Bye! -module(hi_bye). -export(run/0). run ( )-> receive hi → io:format("bye!~n", [ ]), run( ), end. after compiling the module: erlang> PID = spawn(hi_bye, run, [ ]). erlang> PID ! hi.
  • 19. Example actor: a counter (Interactive)
  • 20. Ping! Pong! We have 2 processes: player1 & player2  player2 goes in a loop where it can receive one of 2 messages: − {ping, ReplyToPID} makes player2 respond by sending the message pong to the process denoted by ReplyToPID − stop makes player2 print “finish” on the screen and exit the loop  player1 takes a parameter N and does the following: − repeat N times:  send the tuple {ping, self( )} to player2  receive the atom pong from player2 − send stop to player2 − print "finish" and exit.  player2 will have a name created using register( ), all messages to player2 will use the registered name.  Note: self( ) is a built-in function that returns the ID of the process which called it.
  • 23. Distributed Erlang  Each Erlang system running on a computer is called an Erlang node.  A node can have a name, either a short name or a long name.  The name allows other nodes to access it.  For two Erlang nodes to communicate, they must have the same secret cookie. (for security reasons).
  • 24. Distributed Erlang  Short and long names: − When using short names we assume that all nodes are in the same IP domain and we can use only the first component of the IP address. − If we want to use nodes in different domains we use -name instead for long names, but then all IP address must be given in full.
  • 25. Names and cookies  Names and cookies are passed to Erlang via the command line: erl -sname server -setcookie mysecret OR erl -name server@127.0.0.1 -setcookie mysecret − Note: On Windows you can use erl.exe (console) or werl.exe (GUI).  A running program can read its own node's name with the node() function and can set and get the cookie with the set_cookie(...) and get_cookie( ) functions.
  • 26. Distributed Erlang  So how do we send a message to a process to another node?  We already know!!!  {Name, Node} ! msg  Example: {my_process, 'server@samy-pc' } ! {connect}
  • 27. Final example  A small message board application:  Server: The server will run in an infinite loop and can receive these messages: − {connect Username} to add the user to its user list − {say Username Text} to display a user message − showusers to display the list of connected users.  Client: The client will connect to a server with the {connect,...} message, and then keep sending text to the server via {say, ...}
  • 28. Help: Erlang command line on Windows  Go to start menu → Run  Write: cmd.exe and press enter  From the command line: c:> cd "c:Program fileserl5.7.4bin" <enter> c:...> werl.exe -sname node1 -setcookie mysecret  Note: The name of the node will be shown in the Erlang command line:
  • 29. Let's take a breath
  • 30. More stuff in Erlang...  Error handling using workers, supervisors and supervision trees.  Bit syntax and bit operations  The built-in Mnesia database (and others)  List comprehensions: L = [ {X, Y} || X<- [1,2,3,4], Y <-[1,2,3,4], X+Y==5] L = [ {1, 4}, {2, 3}, {3, 2}, {4,1} ]  Web applications, GUI applications, Networking, OpenGL, other databases...  ....etc