erlang introduction bcberlin3

158
Erlang Introduction 1

Upload: guesta3202

Post on 12-May-2015

3.968 views

Category:

Technology


0 download

TRANSCRIPT

Page 1: Erlang Introduction Bcberlin3

Erlang Introduction

1

Page 2: Erlang Introduction Bcberlin3

2

Page 3: Erlang Introduction Bcberlin3

3

Page 4: Erlang Introduction Bcberlin3

4

Page 5: Erlang Introduction Bcberlin3

5

Page 6: Erlang Introduction Bcberlin3

6

Page 7: Erlang Introduction Bcberlin3

7

Page 8: Erlang Introduction Bcberlin3

8

Page 9: Erlang Introduction Bcberlin3

Syntax

9

Page 10: Erlang Introduction Bcberlin3

Hello User

10

Page 11: Erlang Introduction Bcberlin3

erl

11

Page 12: Erlang Introduction Bcberlin3

$ erl1> |

12

Page 13: Erlang Introduction Bcberlin3

1> io:format("Hello User~n", []).Hello Userok

13

Page 14: Erlang Introduction Bcberlin3

2> 1 + 2008.2009

14

Page 15: Erlang Introduction Bcberlin3

3> 1 + 2 + 3.6

15

Page 16: Erlang Introduction Bcberlin3

10> 1 + 2 * 3.711> 2 * 3 + 1.712> (1 + 2) * 3.9

16

Page 17: Erlang Introduction Bcberlin3

13> 3/0.** exception error: bad argument in an arithmetic expression in operator '/'/2 called as 3 / 0

17

Page 18: Erlang Introduction Bcberlin3

4> catch 3/0.{'EXIT',{badarith,[{erlang,'/',[3,0]}, {erl_eval,do_apply,5}, {erl_eval,expr,5}, {shell,exprs,6}, {shell,eval_exprs,6}, {shell,eval_loop,3}]}}

18

Page 19: Erlang Introduction Bcberlin3

15> "Erlang intro"."Erlang intro"

19

Page 20: Erlang Introduction Bcberlin3

35> "abc" ++ "def"."abcdef"

20

Page 21: Erlang Introduction Bcberlin3

27> [72, 101, 108, 108, 111]."Hello"

21

Page 22: Erlang Introduction Bcberlin3

27> [$E, $r, $l, $a, $n, $g]."Erlang"

22

Page 23: Erlang Introduction Bcberlin3

Atom

23

Page 24: Erlang Introduction Bcberlin3

6> atom.atom

24

Page 25: Erlang Introduction Bcberlin3

oktruefalsekernelerlang'EXIT''Name'

25

Page 26: Erlang Introduction Bcberlin3

6> is_atom(zbar).true

9> 1 == 3.false

26

Page 27: Erlang Introduction Bcberlin3

Use Functions

27

Page 28: Erlang Introduction Bcberlin3

36> lists:reverse("nilreB olleH")."Hello Berlin"

28

Page 29: Erlang Introduction Bcberlin3

1> Mod = lists.lists2> Fn = reverse.reverse3> Mod:Fn("tac")."cat"

29

Page 30: Erlang Introduction Bcberlin3

5> Mod:(list_to_atom("reverse"))("ate"). "eta"

30

Page 31: Erlang Introduction Bcberlin3

37> apply(lists, reverse, ["nilreB"])."Berlin"

31

Page 32: Erlang Introduction Bcberlin3

39> Fn = fun lists:reverse/1.#Fun<lists.reverse.1>40> apply(Fn, ["olleH"])."Hello"

32

Page 33: Erlang Introduction Bcberlin3

Variables

33

Page 34: Erlang Introduction Bcberlin3

48> N = 1.149> N = 2.** exception error: no match of right hand side value 250> N = 1.1

34

Page 35: Erlang Introduction Bcberlin3

Pattern matchingand Variables

35

Page 36: Erlang Introduction Bcberlin3

2> {espresso, _, Crema} = Coffee.{espresso, "Harem", orange}

1> Coffee = harem:espresso().{espresso, "Harem", orange}

3> Crema.orange

36

Page 37: Erlang Introduction Bcberlin3

Data Structures

37

Page 38: Erlang Introduction Bcberlin3

[101, left, right, $a, [0, 1, 2], "abc"]

38

Page 39: Erlang Introduction Bcberlin3

5> L = [101, left, right, "abc"]. [101,left,right,"abc"]

6> lists:nth(1,L). 101

39

Page 40: Erlang Introduction Bcberlin3

7> lists:nthtail(1, L). [left,right,"abc"]

40

Page 41: Erlang Introduction Bcberlin3

29> [66, 101 | "rlin"]."Berlin"

41

Page 42: Erlang Introduction Bcberlin3

38> [First, Sec | Rest] = "Rest"."Rest"

39> First.82

40> [First]."R"41> Sec.10142> [Sec]. "e"

42

Page 43: Erlang Introduction Bcberlin3

Strings sind Listen

43

Page 44: Erlang Introduction Bcberlin3

Tuple

44

Page 45: Erlang Introduction Bcberlin3

{1, 2, 3}

45

Page 46: Erlang Introduction Bcberlin3

1> P = {adam,24,{july,29}}.{adam,24,{july,29}}

2> element(1,P).adam

46

Page 47: Erlang Introduction Bcberlin3

3> element(3,P).{july,29}

47

Page 48: Erlang Introduction Bcberlin3

4> P2 = setelement(2,P,25).{adam,25,{july,29}}

48

Page 49: Erlang Introduction Bcberlin3

5> tuple_size(P).3

49

Page 50: Erlang Introduction Bcberlin3

Binaries

50

Page 51: Erlang Introduction Bcberlin3

<<13, 10, 13, 10>>

51

Page 52: Erlang Introduction Bcberlin3

<<"GET / HTTP/1.1">>

52

Page 53: Erlang Introduction Bcberlin3

web_request(Req) -> Req:ok({"text/html", <<"<html> <head><title> Hello Berlin </title></head> <body> Hello Berlin </body> </html>">>}).

53

Page 54: Erlang Introduction Bcberlin3

54

Page 55: Erlang Introduction Bcberlin3

0000000 d4 c3 b2 a1 02 00 04 00 00 00 00 00 00 00 00 000000010 60 00 00 00 01 00 00 00 07 aa f7 48 b4 32 08 000000020 4e 00 00 00 4e 00 00 00 00 14 6c 62 ef 18 00 190000030 e3 07 c0 bc 08 00 45 00 00 40 30 0d 40 00 40 060000040 3e 4e c0 a8 b2 cb d1 55 87 93 cd b9 00 50 a1 590000050 7f 98 00 00 00 00 b0 02 ff ff ad c0 00 00 02 040000060 05 b4 01 03 03 03 01 01 08 0a 27 c0 a6 25 00 000000070 00 00 04 02 00 00 0000076

55

Page 56: Erlang Introduction Bcberlin3

<<Version:4, IHLen:4, TOService:8, TotalLength:16, Id:16, Flags:3, FragOffset:13, Ttl:8, Protocol:8, HeaderChecksum:16, SourceAddr:32, DestAddr:32>> = H.

56

Page 57: Erlang Introduction Bcberlin3

43> A = 0.044> B = 8.845> C = 15.1546> Msg = <<A:9, B:12, C:16>>.<<0,0,64,0,15:5>>

57

Page 58: Erlang Introduction Bcberlin3

dictarraysetssofs

digraph

58

Page 59: Erlang Introduction Bcberlin3

Module file

59

Page 60: Erlang Introduction Bcberlin3

-module(forest).-export([tree/1, tree/2]).

-include_lib("kernel/include/flora.hrl").

%% External API

tree(Age) -> {tree, Age}.tree(Age, Kind) -> {tree, Age, Kind}.

60

Page 61: Erlang Introduction Bcberlin3

forest.erl

61

Page 62: Erlang Introduction Bcberlin3

$ erlc forest.erl

62

Page 63: Erlang Introduction Bcberlin3

forest.beam

63

Page 64: Erlang Introduction Bcberlin3

Define Functions

64

Page 65: Erlang Introduction Bcberlin3

double(X) -> 2 * X.

greet(Name) -> io:fwrite("Hallo ~p~n", [Name]),

ok.

65

Page 66: Erlang Introduction Bcberlin3

1, 2, 3, 5, 8, 13, 21, 34, 55, 89

66

Page 67: Erlang Introduction Bcberlin3

fib(0) -> 0;fib(1) -> 1;fib(N) when is_integer(N) -> fib(N-1) + fib(N-2).

67

Page 68: Erlang Introduction Bcberlin3

For Want Of Loop

68

Page 69: Erlang Introduction Bcberlin3

Recursion

69

Page 70: Erlang Introduction Bcberlin3

vowels(Text) -> vowels(Text, []).

vowels([H|T], Acc) -> Acc1=case member(H, "aeiuo") of true -> [H | Acc]; false -> Acc end, vowels(T, Acc1);vowels([], Acc) -> reverse(Acc).

70

Page 71: Erlang Introduction Bcberlin3

foo(N) when N > 1 -> ok.

71

Page 72: Erlang Introduction Bcberlin3

bar(N) when is_list(N), length(N) ->

72

Page 73: Erlang Introduction Bcberlin3

baz(I) when I > 0, I < 9 ->

73

Page 74: Erlang Introduction Bcberlin3

quux(K) when I =< 0 or I >= 9 ->

74

Page 75: Erlang Introduction Bcberlin3

double(N) when is_number(N) -> N * 2;double(N) when is_list(N) -> N ++ N.

75

Page 76: Erlang Introduction Bcberlin3

process("/customer" ++ QString) -> customer(QString);

76

Page 77: Erlang Introduction Bcberlin3

process("/customer" ++ QString) -> customer(QString);process("/employee" ++ QString) -> employee(QString);process(Any) -> ok.

77

Page 78: Erlang Introduction Bcberlin3

Concurrent programming

78

Page 79: Erlang Introduction Bcberlin3

Shared state

79

Page 80: Erlang Introduction Bcberlin3

Message passing

80

Page 81: Erlang Introduction Bcberlin3

81

Page 82: Erlang Introduction Bcberlin3

spawn(Fun)

82

Page 83: Erlang Introduction Bcberlin3

Pid = spawn(Fun)

83

Page 84: Erlang Introduction Bcberlin3

Pid ! {lookup, {name, "Carl"}}

84

Page 85: Erlang Introduction Bcberlin3

receive {lookup, Query} -> query(Query)end

85

Page 86: Erlang Introduction Bcberlin3

receive {lookup, Query} -> query(Query); {update, Update} -> update(Update)end

86

Page 87: Erlang Introduction Bcberlin3

receive Any -> Anyend

87

Page 88: Erlang Introduction Bcberlin3

Pid ! Msg.receiver ! Msg.{receiver, '[email protected]'} ! Msg.

88

Page 89: Erlang Introduction Bcberlin3

link(Pid)

89

Page 90: Erlang Introduction Bcberlin3

process_flag(trap_exit, true)

90

Page 91: Erlang Introduction Bcberlin3

{'EXIT', From, Reason}

91

Page 92: Erlang Introduction Bcberlin3

1> self().<0.89.0>

92

Page 93: Erlang Introduction Bcberlin3

Distributed Erlang

93

Page 94: Erlang Introduction Bcberlin3

spawn(Node, Fun)

94

Page 95: Erlang Introduction Bcberlin3

Node = 'serv@flomac'.

95

Page 96: Erlang Introduction Bcberlin3

node()

96

Page 97: Erlang Introduction Bcberlin3

nodes()

97

Page 98: Erlang Introduction Bcberlin3

net_adm:ping(Node)

98

Page 99: Erlang Introduction Bcberlin3

Function Functions

99

Page 100: Erlang Introduction Bcberlin3

lists

100

Page 101: Erlang Introduction Bcberlin3

mapfoldlfilter

101

Page 102: Erlang Introduction Bcberlin3

lists:map(Fun, List)

102

Page 103: Erlang Introduction Bcberlin3

L = [1,2,3,4,5],L1 = lists:map(fun(E) -> E * 2 end, L).[2,4,6,8,10]

103

Page 104: Erlang Introduction Bcberlin3

lists:foldl(Fun, Acc, List)

104

Page 105: Erlang Introduction Bcberlin3

L = [1,2,3,4,5],L1 = lists:foldl(fun(E, Acc) -> Acc + E end, L).

15

105

Page 106: Erlang Introduction Bcberlin3

lists:filter(Fun, List)

106

Page 107: Erlang Introduction Bcberlin3

L = [1,2,3,4,5],L1 = lists:filter(fun(E) -> E rem 2 == 0 end, L).[2,4]

107

Page 108: Erlang Introduction Bcberlin3

108

Page 109: Erlang Introduction Bcberlin3

rpc:pmap({Module, Function}, ExtraArgs, List2)

109

Page 110: Erlang Introduction Bcberlin3

OTP

110

Page 111: Erlang Introduction Bcberlin3

111

Page 112: Erlang Introduction Bcberlin3

Supervision Tree Behaviours

Applications

Releases

Up- & Downgrades

112

Page 113: Erlang Introduction Bcberlin3

113

Page 114: Erlang Introduction Bcberlin3

{ch3, start_link, []}

114

Page 115: Erlang Introduction Bcberlin3

init(_Args) -> {ok, {{one_for_one, 1, 60}, [{ch3, {ch3, start_link, []}, permanent, brutal_kill, worker, [ch3]}] }}.

115

Page 116: Erlang Introduction Bcberlin3

116

Page 117: Erlang Introduction Bcberlin3

Behaviour

117

Page 118: Erlang Introduction Bcberlin3

Impl

Base Class

118

Page 119: Erlang Introduction Bcberlin3

Callback

Behaviour

119

Page 120: Erlang Introduction Bcberlin3

Library

120

Page 121: Erlang Introduction Bcberlin3

kernel

rpc

erlang file

inetapplication io

error_logger

code

121

Page 122: Erlang Introduction Bcberlin3

stdlib

filename

dictmath

stringsets

dets regexpproplists

arrayrandom

queue timer

shell

gen_server

lists

122

Page 123: Erlang Introduction Bcberlin3

123

Page 124: Erlang Introduction Bcberlin3

Do stuff

124

Page 125: Erlang Introduction Bcberlin3

Unit Tests

125

Page 126: Erlang Introduction Bcberlin3

succeed() -> ok.

fail() -> throw(failed).

succeeding_test() -> succeed().

failing_test() -> fail().

126

Page 127: Erlang Introduction Bcberlin3

succeeding_assert_test_() -> ?_assert(1 > 0).

failing_assert_test_() -> ?_assert(0 > 1).

succeeding_error_test_() -> ?_assertError(foo, erlang:error(foo)).

failing_error_test_() -> ?_assertError(foo, erlang:throw(foo)).

127

Page 128: Erlang Introduction Bcberlin3

File IO

128

Page 129: Erlang Introduction Bcberlin3

{ok, Binary} = file:read_file(Filename)

129

Page 130: Erlang Introduction Bcberlin3

{ok, Terms} = file:consult(Filename)

130

Page 131: Erlang Introduction Bcberlin3

Macros

131

Page 132: Erlang Introduction Bcberlin3

?MODULE?LINE

-define(OK, "200").?OK

-define(trace(A), ...)?trace

132

Page 133: Erlang Introduction Bcberlin3

Hello world

133

Page 134: Erlang Introduction Bcberlin3

$ sudo port install mochiweb$ escript /opt/local/share/scripts/ \ new_mochiweb.erl my \ app$ cd myapp ; make$ sh start-dev.sh

$ open localhost:8000

134

Page 135: Erlang Introduction Bcberlin3

135

Page 136: Erlang Introduction Bcberlin3

136

Page 137: Erlang Introduction Bcberlin3

536 SeitenJuli '07

137

Page 138: Erlang Introduction Bcberlin3

138

Page 139: Erlang Introduction Bcberlin3

139

Page 141: Erlang Introduction Bcberlin3

Record

141

Page 142: Erlang Introduction Bcberlin3

-record(person, {name, age}).

142

Page 143: Erlang Introduction Bcberlin3

.hrl

143

Page 144: Erlang Introduction Bcberlin3

new(Name, Age) -> #person{name=Name, age=Age}.

144

Page 145: Erlang Introduction Bcberlin3

name(P) -> P#person.name.

145

Page 146: Erlang Introduction Bcberlin3

rename(P, NewName) -> P#person{name=Name}.

146

Page 147: Erlang Introduction Bcberlin3

Where Next?

147

Page 148: Erlang Introduction Bcberlin3

400 SeitenMärz '09

148

Page 149: Erlang Introduction Bcberlin3

500 SeitenMai '09

149

Page 150: Erlang Introduction Bcberlin3

portsedoc

debuggererlang-mode

disteltype specsdialyzer

mochiwebyaws

Lisp Flavoured ErlangReia

150

Page 151: Erlang Introduction Bcberlin3

OTP Behaviours

151

Page 152: Erlang Introduction Bcberlin3

gen_server

152

Page 153: Erlang Introduction Bcberlin3

gen_fsm

153

Page 154: Erlang Introduction Bcberlin3

supervisor

154

Page 155: Erlang Introduction Bcberlin3

-module(sup).-behaviour(supervisor).

-export([start_link/0]).-export([init/1]).

start_link() -> supervisor:start_link(sup, []).

init(_Args) ->...

155

Page 156: Erlang Introduction Bcberlin3

application

156

Page 157: Erlang Introduction Bcberlin3

filename:dirname( code:which(?MODULE))

157

Page 158: Erlang Introduction Bcberlin3

11> {ok,File} = file:open("PROJECTS", [read]).{ok,<0.92.0>}13> io:get_line(File, [])."Intro Erlang (am Mi., 17.10.08)\n"15> file:close(File).

158