1-module(funs_from_outside). 2 3-export([run1/2, run2/2, run3/2]). 4-export([test1/1, test2/1]). 5 6%%------------------------------------------------------------------------------ 7 8run1(X, Y) -> 9 testa(fun do_something/1, X, Y). 10 11testa(Fun, X, Y) -> 12 F = case even(X) of 13 true -> Fun; 14 false -> fun do_nothing/1 15 end, 16 case F(Y) of 17 {ok, _} -> ok; 18 error -> error 19 end. 20 21do_nothing(_) -> {ok, nothing}. 22 23do_something(_) -> {ok, something}. 24 25even(X) -> 26 X rem 2 =:= 0. 27 28%%------------------------------------------------------------------------------ 29 30%% Duplicating code since we are monovariant... 31 32run2(X, Y) -> 33 testb(fun do_something/1, X, Y). 34 35testb(Fun, X, Y) -> 36 F = case even(X) of 37 true -> Fun; 38 false -> fun do_nothing/1 39 end, 40 case F(Y) of 41 error -> error 42 end. 43 44%%------------------------------------------------------------------------------ 45 46%% Duplicating code since we are monovariant... 47 48run3(X, Y) -> 49 testc(fun do_something_2/1, X, Y). 50 51testc(Fun, X, Y) -> 52 F = case even(X) of 53 true -> Fun; 54 false -> fun do_nothing/1 55 end, 56 case F(Y) of 57 {ok, _} -> ok; 58 %% This pattern can match. 59 error -> error 60 end. 61 62do_something_2(foo) -> {ok, something}; 63do_something_2(_) -> error. 64 65%%------------------------------------------------------------------------------ 66 67test1(Fun) -> 68 F = case get(test1) of 69 test1_t -> Fun; 70 test1_f -> fun fok/0 71 end, 72 error = F(). 73 74fok() -> ok. 75 76%%------------------------------------------------------------------------------ 77 78test2(Fun) -> 79 F = case get(test1) of 80 test1_t -> fun fok/0; 81 test1_f -> fun fok/0 82 end, 83 error = F(). 84