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
|
-module(funs_from_outside).
-export([run1/2, run2/2, run3/2]).
-export([test1/1, test2/1]).
%%------------------------------------------------------------------------------
run1(X, Y) ->
testa(fun do_something/1, X, Y).
testa(Fun, X, Y) ->
F = case even(X) of
true -> Fun;
false -> fun do_nothing/1
end,
case F(Y) of
{ok, _} -> ok;
error -> error
end.
do_nothing(_) -> {ok, nothing}.
do_something(_) -> {ok, something}.
even(X) ->
X rem 2 =:= 0.
%%------------------------------------------------------------------------------
%% Duplicating code since we are monovariant...
run2(X, Y) ->
testb(fun do_something/1, X, Y).
testb(Fun, X, Y) ->
F = case even(X) of
true -> Fun;
false -> fun do_nothing/1
end,
case F(Y) of
error -> error
end.
%%------------------------------------------------------------------------------
%% Duplicating code since we are monovariant...
run3(X, Y) ->
testc(fun do_something_2/1, X, Y).
testc(Fun, X, Y) ->
F = case even(X) of
true -> Fun;
false -> fun do_nothing/1
end,
case F(Y) of
{ok, _} -> ok;
%% This pattern can match.
error -> error
end.
do_something_2(foo) -> {ok, something};
do_something_2(_) -> error.
%%------------------------------------------------------------------------------
test1(Fun) ->
F = case get(test1) of
test1_t -> Fun;
test1_f -> fun fok/0
end,
error = F().
fok() -> ok.
%%------------------------------------------------------------------------------
test2(Fun) ->
F = case get(test1) of
test1_t -> fun fok/0;
test1_f -> fun fok/0
end,
error = F().
|