-
Notifications
You must be signed in to change notification settings - Fork 0
/
erlang.erl
169 lines (151 loc) · 4.37 KB
/
erlang.erl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
-module(test).
-compile(export_all).
%-export([add/2]).
% Builtins
% date() time() length([1,2,3]) size({1,2,3})
% atom_to_list(atom) => "atom"
% list_to_tuple([1,2,3]) => {1,2,3}
% tuple_to_list({1,2,3}) => [1,2,3]
% integer_to_list(1234) => "1234"
% lists module
% hd(List) -> Element -- Returns the first element of the list.
% tl(List) -> List -- Returns the list minus its first element.
% length(List) -> Integer -- returns the length of the list.
% all(Pred, List) -> bool()
% any(Pred, List) -> bool()
% append(List1, List2) -> List3 is equivalent to A ++ B.
% filter(Pred, List1) -> List2
% lists:filter(fun(X) -> X =< 3 end, [3, 1, 4, 1, 6]). % Result is [3,1,1]
% flatten(DeepList) -> List -- Returns a flattened version of DeepList.
% foldl(F, I, L) = F(L_n, ... F(L_2, F(L_1, I)))
% foldr(F, I, L) = F(L_1, F(L_2, ... F(L_n, I)))
% foldl(Fun, Acc0, List) -> Acc1
% Example: lists:foldl(fun(X, Y) -> X + 10 * Y end, 0, [1, 2, 3]). % Result is 123
% foreach(Fun, List) -> void()
% map(Fun, List1) -> List2
% Example: lists:map(fun(X) -> 2 * X end, [1, 2, 3]). % Result is [2,4,6]
% member(Elem, List) -> bool() -- Search Elem in List
% partition(Pred, List) -> {Satisfying, NonSatisfying}
% reverse(List1) -> List2
% seq(From, To) -> Seq -- [From, To], inclusive.
% seq(From, To, Incr) -> Seq
% sort(List1) -> List2 -- Returns a list containing the sorted elements of List1.
% unzip(List1) -> {List2, List3}
% zip(List1, List2) -> List3 -- L1 and L2 of the same length
% Variables (UpperCamelCase)
diff(A, B) ->
C = A - B,
C.
% Functions
add(A, B) ->
A + B.
% Definition via pattern, note the ';'
fact(0) -> 1;
fact(N) when N > 0 -> N * fact(N-1).
area({square, Side}) -> Side * Side;
area({circle, Radius}) -> 3.14 * Radius * Radius;
area({triangle, A, B, C}) ->
S = (A + B + C) / 2,
math:sqrt(S*(S-A)*(S-B)*(S-C)).
% Guards after `when`:
% is_number(X) is_integer(X) is_float(X) is_atom(X) is_tuple(X) is_list(X)
% X > Y + Z
% X =< Y (not <=)
% A and B (or/not/...)
% X =:= Y <=> X === Y
% X == Y <=> equal with float-int coercion
% X =/= Y <=> X !== Y
% Case
casef(X) ->
case lists:member(a, X) of
true -> present;
false -> absent
end.
iff(X) ->
if
is_integer(X) -> integer;
is_tuple(X) -> tuple;
true -> boh
end.
% Symbols (lowercase or 'escaped')
sym() ->
ciao.
% Tuples
tuple() ->
{123, abc, {a, b}}.
% List
list() ->
[1, 2, 3] ++ [4, 5].
cons(X, L) ->
[X | L].
% List comprehensions
comp() ->
[{X,Y} || X <- [-1,0,1], Y <- [one, two], X >= 0].
% => [{0,one},{1,one},{0,two},{1,two}]
% Matching
match() ->
[A,B|C] = [1,2,3,4], % => A=1, B=2, C=[3,4]
{D,E,_} = {1,2,3}. % D=1, E=2
% Maps
map() ->
Map = #{one => 1, "Two" => 2, 3 => three},
Map#{one := "uno"}, % return the updated, keep Map unchanged
#{"Two" := V} = Map,
V. % => 2
% apply(Mod, Func, Args)
apply() ->
apply(?MODULE, add, [1,2]). % => 3
% Lambda
lambda() ->
fun(X) -> X*X end.
lambda2() ->
(lambda())(9), % => 81
lists:map(lambda(), [1,2,3]), % => [1,4,9]
lists:foldr(fun add/2, 0, [1,2,3]). % => 6
% Processes and messages
% Between 2 processes there is FIFO ordering of messages
actor1() ->
Pid2 = spawn(?MODULE, actor2, []),
Pid2 ! {self(), foo}.
actor2() ->
receive
{From, Msg} -> io:format("Actor2 received ~w from ~w~n", [Msg, From])
end.
actor2withTimeout(T) ->
receive
{From, Msg} -> io:format("Actor2 received ~w from ~w~n", [Msg, From])
after
T -> io:format("Timeout!") % T in ms
end.
% Management of processes
startMaster() ->
Pid = spawn(?MODULE, masterBody, []),
register(master, Pid).
startWorker() ->
spawn_link(?MODULE, workerBody, []).
workerBody() ->
BadLuck = rand:uniform(100) =< 30,
if
BadLuck -> exit("Bye.");
true -> master ! {done}
end.
masterBody() ->
startWorker(),
startWorker(),
startWorker(),
handleExits().
handleExits() ->
process_flag(trap_exit, true),
receive
{'EXIT', Pid, normal} ->
io:format("Process ~p exited normally~n", [Pid]),
handleExits();
{'EXIT', Pid, Msg} ->
io:format("Process ~p died with message: ~s~n",
[Pid, Msg]),
spawn_link(?MODULE, workerBody, []),
handleExits();
{done} ->
io:format("Worker done~n", []),
handleExits()
end.