-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.erl
236 lines (175 loc) · 6.42 KB
/
utils.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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
%% @author Karl Marklund <[email protected]>
%% @doc A small collection of utility functions.
-module(utils).
-export([seqs/1, filter/2, split/2]).
%% To use EUnit we must include this.
-include_lib("eunit/include/eunit.hrl").
-compile(export_all).
%% @doc Generates a list of lists of increasing sequences of integers
%% starting with the empty list and ending with [1,2, ..., N].
%% === Example ===
%% <div class="example">```
%% > utils:seqs(5).
%% [[],[1],[1,2],[1,2,3],[1,2,3,4],[1,2,3,4,5]]'''
%% </div>
-spec seqs(N::integer()) -> [[integer()]].
seqs(N) ->
%% NOTE: Simply using a list comprehension such as [[]] ++
%% [lists:seq(1,M) || M <- lists:seq(1,N)] will be quite slow
%% since each sequence is generated from scratch. Hence, lets
%% re-use the last sequnece and add a new element when
%% constructing the next sequence.
F = fun(X,[H|T]) -> [[X|H],H|T] end,
lists:foldl(F, [[]], lists:seq(1,N)),
lists:reverse([lists:reverse(L) || L <- lists:foldl(F, [[]], lists:seq(1,N))]).
%% @doc Each list in List2 contains the elements Elem in List1 for
%% which one of the Pred(Elem) returns true. The order of the lists in
%% List2 is the same as the order of the predicates. In each list in
%% List2, the relative order of the elements are the same as in the
%% original List1.
%%
%% === Example ===
%% <div class="example">```
%% 1> L = [1,2,3,4,5,6,7,8,9,10].
%% [1,2,3,4,5,6,7,8,9,10]
%% 2> P1 = fun(X) -> X rem 2 == 1 end.
%% #Fun<erl_eval.6.111823515>
%% 3> P2 = fun(X) -> not P1(X) end.
%% #Fun<erl_eval.6.111823515>
%% 4> P3 = fun(X) -> X > 3 andalso X < 7 end.
%% #Fun<erl_eval.6.111823515>
%% 5> utils:filter([P1,P2,P3], L).
%% [[1,3,5,7,9],[2,4,6,8,10],[4,5,6]]'''
%% </div>
-spec filter(Preds, List1) -> List2 when
Preds :: [Pred],
Pred :: fun((Elem :: T) -> boolean()),
List1 :: [T],
List2 :: [[T]],
T :: term().
filter(Predicates, List) ->
Collect = self(),
[spawn(fun() -> Collect!{I,lists:filter(P,List)} end) ||
{I, P} <- lists:zip(lists:seq(1, length(Predicates)), Predicates)],
filter_collect(length(Predicates), []).
filter_collect(0,R) ->
[L || {_,L} <- lists:sort(R)];
filter_collect(N,R) ->
receive
{I, L} -> filter_collect(N-1, [{I,L}|R])
end.
lqr(L, N) ->
Len = length(L),
%% Quotient
Q = Len div N,
%% Reminder
R = Len rem N,
{Len, Q, R}.
%% @doc Split List into N Lists such that all Lists have approximately the same number of elements.
%%
%% Let Len = length(List), Q = Len div N and R = Len rem N.
%%
%% If R = 0, then all of the lists in Lists will be of length Q.
%%
%% If R =/= 0, then R of the lists in Lists will have
%% lenght Q + 1.
%%
%% === Example ===
%%
%% <div class="example">```
%% 1> L = [1,2,3,4,5,6,7,8,9,10].
%% [1,2,3,4,5,6,7,8,9,10]
%% 2> utils:split(L, 4).
%% [[1,2],[3,4],[5,6,7],[8,9,10]]
%% 3> lists:concat(utils:split(L,3)).
%% [1,2,3,4,5,6,7,8,9,10]'''
%% </div>
-spec split(List, N) -> Lists when
List :: [T],
Lists :: [List],
T :: term(),
N :: integer().
split(L, N) ->
tbi.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% %%
%% EUnit Test Cases %%
%% %%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
seqs_length_test_() ->
%% The list [[], [1], [1,2], ..., [1,2, ..., N]] will allways have
%% length N+1.
[?_assertEqual(N+1, length(seqs(N))) || N <- lists:seq(1, 55)].
seqs_test_() ->
%% A small collection of expected results {N, seqs(N)}.
Data = [{0, [[]]}, {1, [[], [1]]}, {2, [[], [1], [1,2]]},
{7, [[],
[1],
[1,2],
[1,2,3],
[1,2,3,4],
[1,2,3,4,5],
[1,2,3,4,5,6],
[1,2,3,4,5,6,7]]}
],
[?_assertEqual(L, seqs(N)) || {N, L} <- Data].
filter_test_() ->
[?_assertEqual([], filter([], L)) || L <- seqs(10)].
filter_true_false_test_() ->
P1 = fun(_) -> false end,
P2 = fun(_) -> true end,
P3 = fun(X) -> X rem 2 == 0 end,
Expected = fun(L) -> [lists:filter(P,L) || P <- [P1,P2,P3]] end,
[?_assertEqual(Expected(L), filter([P1,P2,P3], L) ) || L <- seqs(10) ].
filter_test() ->
L = lists:seq(1,10),
P1 = fun(X) -> X rem 2 == 0 end,
P2 = fun(X) -> X rem 2 == 1 end,
P3 = fun(X) -> X > 3 end,
%%E = [[2,4,6,8,10],[1,3,5,7,9],[4,5,6,7,8,9,10]],
E = [lists:filter(P,L) || P <- [P1,P2,P3]],
?assertEqual(E, filter([P1,P2,P3], L)).
split_concat_test_() ->
%% Make sure the result of concatenating the sublists equals the
%% original list.
L = lists:seq(1,99),
[?_assertEqual(L, lists:concat(split(L,N))) || N <- lists:seq(1,133)].
split_n_test_() ->
%% Make sure the correct number of sublists are generated.
M = 99,
L = lists:seq(1,M),
Num_of_lists = fun(List, N) when N =< length(List) ->
N;
(List, _) ->
length(List)
end,
[?_assertEqual(Num_of_lists(L,N), length(split(L,N))) || N <- L].
expected_stat(L, N) when N =< length(L) ->
%% When spliting a list L into N sublists, we know there will only by two possible
%% lengths of the sublists.
%% Quotient and reminder when dividing length of L with N.
{_, Q, R} = lqr(L, N),
%% There will allways be R sublists of length Q+1 and N-R sublists
%% of length Q.
{{R, Q+1}, {N-R, Q}};
expected_stat(L, _N) ->
%% N greater than the length of L, hence all sublists will have
%% length 1.
{{length(L), 1}, {0,0}}.
stat(N, M, LL) ->
%% Return a tuple {{Num_N, N}, {Num_M, M}} where Num_N is the
%% number of lists of length N in LL and Num_M is the number of
%% lists of length M in LL.
S = filter([fun(X) -> X == N end, fun(X) -> X == M end], [length(L) || L <- LL]),
[Num_N, Num_M] = [length(L) || L <- S],
{{Num_N, N}, {Num_M, M}}.
split_stat_test_() ->
%% Assure the list of sublists contains the correct number of
%% lists of the two expected lengths.
Assert = fun(L,N) ->
{_, Q, _} = lqr(L,N),
?_assertEqual(expected_stat(L,N), stat(Q+1, Q, split(L,N)))
end,
%% Generators can depend on other generator expressions, here N
%% depends on the length of L.
[Assert(L,N) || L <- seqs(33), N <- lists:seq(1,length(L)+5)].