erlang noah dietrich chris ruffalo
Post on 17-Jan-2016
220 Views
Preview:
TRANSCRIPT
Erlang
http://erlang.org/
Noah Dietrich
Chris Ruffalo
2
Introduction to Erlang
• Designed by Ericson for phone switches.
• Released Open Source in 1998.
• Interpreted VM code.
3
Strengths & Uses
• Distributed, fault-tolerant, soft-real-time.
• Hot-swap code
• Lightweight threading, SMP.
4
Using Erlang
• Open Source – Download from http://erlang.org
• Read-Eval loop
• Run Erlang from your source directory:
5
Variables: Numbers
• Terms
• Integer
• Float
6
Examples: Numbers
>1 + 1.
2
>1000000000000000000000 + 3.
1000000000000000000003
> .1 +2.
2.1
7
Variables: Continued
• Atoms
• Tuples
• Lists
• Bit String
8
ExamplesExample 1:> myAtom.
myAtom
> ‘this is also an atom’.
'this is also an atom'
Example 2:>X = {person, 'bob smith'}.
{person, 'bob smith'}
> {person, Who} = X.
{person, 'bob smith'}
>Who.
'bob smith'
Example 3:
> Y = {person, {name, 'bob smith'}, {age, 22} }.{person, {name, 'bob smith'}, {age, 22} }
> {_, {_, _} , {_, ClientAge } = Y.{person, {name, 'bob smith'}, {age, 22} }
> ClientAge.22
9
Working With Variables
• Single Assignment – one value for lifetime of variable.
• Pattern matching – Equals sign is not assignment, but pattern match.
• Infix Notation
• Short-circuit Evaluation
• Type conversion
10
Examples
• Short-Circuit Expressions
– Expression_1 orelse Expression_2
– Expression_1 andalso Expression_2
• Type Conversions With BIF's
> atom_to_list(hello).
"hello"
> list_to_atom("hello").
hello
11
Advanced Types
• PID (process ID)
• Port
• Reference
• Fun (anonymous function)
12
Not Types
• Boolean
• Char
• String
• Record
13
ExamplesLists:
"hello" is shorthand for the list [$h,$e,$l,$l,$o],
which is really: [104,101,108,108,111].
Boolean:1> 2 =< 3.
true
2> true or false.
true
14
Control Statements
• If
• Case
• Guards
• No For-loop
15
Guards
• Used in conditional statements.
• Guard Expression:
• Guard
• Guard Sequence
16
Guard ExamplesGuard Expressions:
X > 2
Y = 4
Guard (true if ALL guard expressions are true):
X > 2, Y = 4, N >= 2
Guard Sequence (true if One guard is true)
X > 2, Y = 4, N >= 2 ; is_float(W), W > 0
17
If Expression
if
GuardSeq1 ->
Body1;
...;
GuardSeqN ->
BodyN;
true ->
% works as an else branch
end.
18
Case Expression
case Expr of
Pattern1 [when GuardSeq1] ->
Body1;
...;
PatternN [when GuardSeqN] ->
BodyN
_Else ->
BodyElse;
end.
19
For Loop
• Erlang does not support looping
• Use recursive function calls instead.
20
Memory Management
• Garbage Collection
• Threads and Memory Overhead
• Activation Records
21
Modules
• Contain attributes and functions
• Expose interface
• Compiled into .beam files (object code)
• Compiled & loaded from interpreter with – > c(Module)
22
Module Example-module(m).
-export([fact/1]).
fact(N) when N>0 -> N * fact(N-1);
fact(0) -> 1.
Save this code in a file called m.erl
Compile in Erlang shell with: c(m).
c stands for compile. Make sure to run Erlang interpreter from the same directory as where your code is saved.
Run factorial in Erlang shell by calling: m.fact(20).
23
Functions
• Pattern match implementation
• First-class functions
• Tail Recursion
• Use Guard Sequences to identify correct clause
24
Function Example-module(euler).
-export([compute/1]).
%% Find the sum of all the multiples of 3 or 5 below 1000.
compute(N) when N >= 1000 -> 0;
compute(N) when N rem 5 =:= 0;N rem 3 =:= 0 -> N + compute (N+1)
compute(N) -> compute (N + 1).
25
Functional Object (Anonymous Functions)
1> Fun1 = fun (X) -> X+1 end.
#Fun<erl_eval.6.39074546>
2> Fun1(2).
3
26
List Operations
• Erlang has many methods for operating on or across lists.– Maps– For Each– Folding– Comprehensions
27
Mapping
• Erlang allows developers to use a special built in function in the lists module to apply a function across an entire list.
• Can be used with normally declared functions or with anonymous functions.
28
Mapping Example
Code:raise_list(List) ->
lists:map(fun pow/1,List).pow({X,Y}) -> math:pow(X,Y).Input:
raise_list([{1,2},{3,4},{4,5}]).Output:
[1.0,81.0,1024.0]
29
For Each
• The foreach function in the lists module is similar to map, except that no value is returned.
• Good for things like sending messages to threads or sockets where no (direct) return is expected.
30
Folding
• The foldl and foldr functions in the lists module can be used for situations where an accumulator must be used instead of a list return.
• Sum, average, counts, etc.• The l and r that proceed the fold indicate
the directionality.• The foldl function is preferred over foldr
because it is tail recursive.
31
Folding Example
Code:sum(List) -> lists:foldl( fun (Element, Acc) -> Acc + Element end, 0, List ).Input:sum([1,2,3])Output:6
32
Comprehensions
• Used to generate one list from another.
• Can be used as shorthand for a map or foreach.
33
Comprehension Example
Code:
raise_list(List) ->
[math:pow(X,Y) || {X,Y} <- List].
Input:
raise_list([{1,2},{3,4},{4,5}])
Output:
[1.0,81.0,1024.0]
34
Threads
• Erlang is incredibly efficient at creating threads. (An order of magnitude over Java)
• Erlang’s threading system is designed to prevent side effects– Race Condition– Deadlock
• The threading system is an abstraction on a message system and individual message queues.
35
Performance
Code:mark(0) -> {done};mark(X) -> spawn(threadmark,thread,[]), mark(X - 1).
thread() -> {ok}.
• This starts threads that do basically nothing, as a benchmark on thread creation.
• All threaded methods should be tail recursive. Non tail-recursive methods will quickly cause resource issues.
36
Side Effects
• Race Condition– Locking
• Deadlock
• Bottom line: Erlang avoids side effects by not including the constructs that are prone to causing them.
37
You’ve Got Mail
• Threads can receive messages. Each spawned thread has its own “mailbox”. Retrieving a message from the mailbox is triggered by the “receive” keyword.
• The mailbox is an abstraction over a messaging queue.
• The order that messages are received dictates their processing order (no way to skip, reorder, or discard messages).
38
Thread Example
Code:sumthread(Sum) -> io:fwrite("Waiting ~b~n",[Sum]), receive done -> io:fwrite("Was sent 'done': ~b~n",
[Sum]); N -> sumthread(Sum + N) end.
39
Thread Example (Cont.)
• Starting a thread:– Pid = spawn(module,sumthread,[0]).
• Sending a message:– Pid ! 12.
• Caveats:– Sending message to a thread that does not exist
will not produce an error.– Sending a message that a thread does not have
a condition for will cause the thread to end.
40
IO
• Erlang allows developers to interact outside of the application by using IO Devices.
• The same methods are used for console and file IO, differing only on the device used.
• First, a device pointer must be obtained.– {ok, Device} = file:open(“file.txt”,[read]),
• Now IO can be performed on the device.
41
IO Example
Code:readFile(File) -> {ok, Device} = file:open(File,[read]), read(Device).
read(Device) -> case io:get_line(Device, "") of eof -> file:close(Device); Line -> io:fwrite("~s",[Line]), read(Device) end.
42
Easy TokenizerCode:readFile(File) -> {ok, Device} = file:open(File,[read]), read(Device).
read(Device) -> case io:get_line(Device, "") of eof -> file:close(Device); Line -> Tokens = string:tokens(Line," "), lists:map( fun (T) -> io:fwrite("~s~n",[T]) end, Tokens ), read(Device) end.
43
Network IO
• Slightly Different than FileIO
• Blocking on listening
• Accept/Process message in another thread
• Binary data format.
44
Network RepeaterCode:% Call echo:listen(Port) to start the service.listen(Port) -> {ok, LSocket} = gen_tcp:listen(Port, ?TCP_OPTIONS), accept(LSocket).
% Wait for incoming connections and spawn the echo loop when we get one.accept(LSocket) -> {ok, Socket} = gen_tcp:accept(LSocket), spawn(fun() -> loop(Socket) end), accept(LSocket).
% Echo back whatever data we receive on Socket.loop(Socket) -> case gen_tcp:recv(Socket, 0) of {ok, <<"quit\n">>} -> gen_tcp:send(Socket, "done"), get_tcp:close(Socket); {ok, Data} -> gen_tcp:send(Socket, Data), loop(Socket); {error, closed} -> ok end.
45
Error Handling
• Try / Catch
• Accepted form of telling a developer that something went wrong.
• Do not rely on status codes in failure situations.
• Ex:– Java: “abcd”.indexOf(“f”) returns -1– Erlang: should throw an exception
46
Homework
• Write a module with a function that determines the sum of all the prime numbers less than a starting number.
• Write a module that solves the problem: what is the sum of the digits of the number 2^(1000)?
• Write a module that reverses the contents of a file and writes it to the console. (Optional Bonus: to another file)
47
Questions?
top related