Top Banner
Erlang http://erlang.org/ Noah Dietrich Chris Ruffalo
47

Erlang Noah Dietrich Chris Ruffalo.

Jan 17, 2016

Download

Documents

Sharlene Martin
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Erlang  Noah Dietrich Chris Ruffalo.

Erlang

http://erlang.org/

Noah Dietrich

Chris Ruffalo

Page 2: Erlang  Noah Dietrich Chris Ruffalo.

2

Introduction to Erlang

• Designed by Ericson for phone switches.

• Released Open Source in 1998.

• Interpreted VM code.

Page 3: Erlang  Noah Dietrich Chris Ruffalo.

3

Strengths & Uses

• Distributed, fault-tolerant, soft-real-time.

• Hot-swap code

• Lightweight threading, SMP.

Page 4: Erlang  Noah Dietrich Chris Ruffalo.

4

Using Erlang

• Open Source – Download from http://erlang.org

• Read-Eval loop

• Run Erlang from your source directory:

Page 5: Erlang  Noah Dietrich Chris Ruffalo.

5

Variables: Numbers

• Terms

• Integer

• Float

Page 6: Erlang  Noah Dietrich Chris Ruffalo.

6

Examples: Numbers

>1 + 1.

2

>1000000000000000000000 + 3.

1000000000000000000003

> .1 +2.

2.1

Page 7: Erlang  Noah Dietrich Chris Ruffalo.

7

Variables: Continued

• Atoms

• Tuples

• Lists

• Bit String

Page 8: Erlang  Noah Dietrich Chris Ruffalo.

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

Page 9: Erlang  Noah Dietrich Chris Ruffalo.

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

Page 10: Erlang  Noah Dietrich Chris Ruffalo.

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

Page 11: Erlang  Noah Dietrich Chris Ruffalo.

11

Advanced Types

• PID (process ID)

• Port

• Reference

• Fun (anonymous function)

Page 12: Erlang  Noah Dietrich Chris Ruffalo.

12

Not Types

• Boolean

• Char

• String

• Record

Page 13: Erlang  Noah Dietrich Chris Ruffalo.

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

Page 14: Erlang  Noah Dietrich Chris Ruffalo.

14

Control Statements

• If

• Case

• Guards

• No For-loop

Page 15: Erlang  Noah Dietrich Chris Ruffalo.

15

Guards

• Used in conditional statements.

• Guard Expression:

• Guard

• Guard Sequence

Page 16: Erlang  Noah Dietrich Chris Ruffalo.

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

Page 17: Erlang  Noah Dietrich Chris Ruffalo.

17

If Expression

if

GuardSeq1 ->

Body1;

...;

GuardSeqN ->

BodyN;

true ->

% works as an else branch

end.

Page 18: Erlang  Noah Dietrich Chris Ruffalo.

18

Case Expression

case Expr of

Pattern1 [when GuardSeq1] ->

Body1;

...;

PatternN [when GuardSeqN] ->

BodyN

_Else ->

BodyElse;

end.

Page 19: Erlang  Noah Dietrich Chris Ruffalo.

19

For Loop

• Erlang does not support looping

• Use recursive function calls instead.

Page 20: Erlang  Noah Dietrich Chris Ruffalo.

20

Memory Management

• Garbage Collection

• Threads and Memory Overhead

• Activation Records

Page 21: Erlang  Noah Dietrich Chris Ruffalo.

21

Modules

• Contain attributes and functions

• Expose interface

• Compiled into .beam files (object code)

• Compiled & loaded from interpreter with – > c(Module)

Page 22: Erlang  Noah Dietrich Chris Ruffalo.

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).

Page 23: Erlang  Noah Dietrich Chris Ruffalo.

23

Functions

• Pattern match implementation

• First-class functions

• Tail Recursion

• Use Guard Sequences to identify correct clause

Page 24: Erlang  Noah Dietrich Chris Ruffalo.

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).

Page 25: Erlang  Noah Dietrich Chris Ruffalo.

25

Functional Object (Anonymous Functions)

1> Fun1 = fun (X) -> X+1 end.

#Fun<erl_eval.6.39074546>

2> Fun1(2).

3

Page 26: Erlang  Noah Dietrich Chris Ruffalo.

26

List Operations

• Erlang has many methods for operating on or across lists.– Maps– For Each– Folding– Comprehensions

Page 27: Erlang  Noah Dietrich Chris Ruffalo.

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.

Page 28: Erlang  Noah Dietrich Chris Ruffalo.

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]

Page 29: Erlang  Noah Dietrich Chris Ruffalo.

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.

Page 30: Erlang  Noah Dietrich Chris Ruffalo.

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.

Page 31: Erlang  Noah Dietrich Chris Ruffalo.

31

Folding Example

Code:sum(List) -> lists:foldl( fun (Element, Acc) -> Acc + Element end, 0, List ).Input:sum([1,2,3])Output:6

Page 32: Erlang  Noah Dietrich Chris Ruffalo.

32

Comprehensions

• Used to generate one list from another.

• Can be used as shorthand for a map or foreach.

Page 33: Erlang  Noah Dietrich Chris Ruffalo.

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]

Page 34: Erlang  Noah Dietrich Chris Ruffalo.

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.

Page 35: Erlang  Noah Dietrich Chris Ruffalo.

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.

Page 36: Erlang  Noah Dietrich Chris Ruffalo.

36

Side Effects

• Race Condition– Locking

• Deadlock

• Bottom line: Erlang avoids side effects by not including the constructs that are prone to causing them.

Page 37: Erlang  Noah Dietrich Chris Ruffalo.

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).

Page 38: Erlang  Noah Dietrich Chris Ruffalo.

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.

Page 39: Erlang  Noah Dietrich Chris Ruffalo.

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.

Page 40: Erlang  Noah Dietrich Chris Ruffalo.

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.

Page 41: Erlang  Noah Dietrich Chris Ruffalo.

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.

Page 42: Erlang  Noah Dietrich Chris Ruffalo.

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.

Page 43: Erlang  Noah Dietrich Chris Ruffalo.

43

Network IO

• Slightly Different than FileIO

• Blocking on listening

• Accept/Process message in another thread

• Binary data format.

Page 44: Erlang  Noah Dietrich Chris Ruffalo.

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.

Page 45: Erlang  Noah Dietrich Chris Ruffalo.

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

Page 46: Erlang  Noah Dietrich Chris Ruffalo.

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)

Page 47: Erlang  Noah Dietrich Chris Ruffalo.

47

Questions?