-
Notifications
You must be signed in to change notification settings - Fork 1
/
mayo.erl
199 lines (184 loc) · 5.45 KB
/
mayo.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
%% -------------------------------------------------------------------
%%
%% Copyright (c) 2011 Bryan Fink
%%
%% This file is provided to you under the Apache License,
%% Version 2.0 (the "License"); you may not use this file
%% except in compliance with the License. You may obtain
%% a copy of the License at
%%
%% http://www.apache.org/licenses/LICENSE-2.0
%%
%% Unless required by applicable law or agreed to in writing,
%% software distributed under the License is distributed on an
%% "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
%% KIND, either express or implied. See the License for the
%% specific language governing permissions and limitations
%% under the License.
%%
%% -------------------------------------------------------------------
%% @doc Recursion help for anonymous functions and alternate control.
%%
%% When you're stuck at the Erlang console, wishing you could
%% write a recursive anonymous function to debug some data
%% structure, or you need a different kind of control than
%% {@link lists} offers, load `mayo'.
%%
%% For instance, you may want to sum mailbox messages until some
%% signal:
%% ```
%% sum_until(Marker) ->
%% mayo:loop(
%% fun(Sum) ->
%% receive
%% Marker -> {stop, Sum};
%% Number -> {continue, Number+Sum}
%% end
%% end,
%% 0).
%% '''
%%
%% Source code may be viewed at
%% [http://github.com/beerriot/mixers/blob/master/src/mayo.erl]
-module(mayo).
-export([
while/3,
do/3,
loop/2
]).
-ifdef(TEST).
-include_lib("eunit/include/eunit.hrl").
-endif.
%% @doc Evaluate `Fun(Acc)' until `Pred(Acc)' returns false. `Init'
%% is passed as `Acc' for the first round, and the result of
%% `Fun(Acc)' is passed as `Acc' subsequent rounds (similar to
%% the accumulator of a fold). The return value is the result of
%% the last evaluation of `Fun(Acc)', or `Init' if `Pred(Init)'
%% returned false.
%%
%% This function is similar to the psuedo code:
%% ```
%% Acc = Init;
%% while( Pred(Acc) ) {
%% Acc = Fun(Acc);
%% }
%% return Acc;
%% '''
-spec while(fun((term()) -> boolean()),
fun((term()) -> term()),
term())
-> term().
while(Pred, Fun, Init) ->
case Pred(Init) of
true ->
while(Pred, Fun, Fun(Init));
false ->
Init
end.
%% @doc Evaluate `Fun(Acc)' until `Pred(Acc)' returns false. Similar
%% to {@link while/3}, except that `Fun' is evaluated before
%% `Pred', so the final result will be either the last result of
%% `Fun(Acc)' or the result of `Fun(Init)' (instead of an
%% unprocessed `Init').
%%
%% For example, "100!" (factorial) might be computed as:
%% ```
%% element(2,
%% do(fun({N, T}) -> {N-1, N*T} end,
%% fun({N, _}) -> N > 1 end,
%% {100, 1})).
%% '''
-spec do(fun((term()) -> boolean()),
fun((term()) -> term()),
term())
-> term().
do(Fun, Pred, Init) ->
Next = Fun(Init),
case Pred(Next) of
true ->
do(Fun, Pred, Next);
false ->
Next
end.
%% @doc Evaluate `Fun(Acc)' as long as it returns `{continue,
%% Acc::term()}', and stop when it returns `{stop, Result::term()}',
%% returning `Result'.
%%
%% For example, draining a process's mailbox can be as simple as:
%% ```
%% loop(fun(A) ->
%% receive X -> {continue, [X|A]}
%% after 0 -> {stop, lists:reverse(A)} end
%% end,
%% [])
%% '''
-spec loop(fun((term()) -> {continue | stop, term()}), term()) -> term().
loop(Fun, Init) ->
case Fun(Init) of
{continue, Next} ->
loop(Fun, Next);
{stop, Result} ->
Result
end.
%%%
-ifdef(TEST).
%% spin a process dict value down to zero
while_test() ->
Key = '$while_test_val',
Initial = 10,
put(Key, Initial),
while(fun(Expected) ->
?assertEqual(get(Key), Expected),
get(Key) > 0
end,
fun(Expected) ->
put(Key, get(Key)-1),
Expected-1
end,
Initial),
?assertEqual(0, get(Key)).
%% spin a process dict value down to zero
do_test() ->
Key = '$do_test_val',
Initial = 10,
put(Key, Initial),
do(fun(Expected) ->
put(Key, get(Key)-1),
Expected-1
end,
fun(Expected) ->
?assertEqual(get(Key), Expected),
get(Key) > 0
end,
Initial),
?assertEqual(0, get(Key)).
%% check that while never runs its fun if Pred(Init) was false
while_never_test() ->
Key = '$while_never_test',
put(Key, pass),
while(fun(_) -> false end,
fun(_) -> put(Key, fail) end,
undefined),
?assertEqual(pass, get(Key)).
%% check that do runs its fun even if Pred(Init) was true
do_always_test() ->
Key = '$do_always_test',
put(Key, fail),
do(fun(_) -> put(Key, pass) end,
fun(_) -> false end,
undefined),
?assertEqual(pass, get(Key)).
%% try that mailbox-draining example
loop_test() ->
loop_test_drain(), %% clear before start
Msgs = ['$loop_test_1', '$loop_test_2', '$loop_test_3'],
[ self() ! M || M <- Msgs ],
Drain = loop_test_drain(),
?assertEqual(Msgs, Drain).
loop_test_drain() ->
loop(fun(A) ->
receive X -> {continue, [X|A]}
after 0 -> {stop, lists:reverse(A)} end
end,
[]).
-endif.