%% @author alexeikrasnopolski %% @doc @todo Add description to monad_test. -module(maybe_monad_2_test). -include_lib("eunit/include/eunit.hrl"). %% ==================================================================== %% API functions %% ==================================================================== -export([continuation/1, f1/1, f2/1, f3/1]). bind(nothing, _C) -> nothing; bind(F, C) -> C(F). return() -> fun(A) -> A end. %% continuation([f1, f2, f3]) -> %% bind(f1, %% bind(f2, %% bind(f3, %% return) %% ) %% ) == %% . %% continuation([f1, f2, f3]) -> %% fun(X) -> bind(f1(X), continuation(f2, f3)) end. %% fun(X) -> bind(f1(X), fun(Y) -> bind(f2(Y), continuation(f3)) end) end. %% fun(X) -> bind(f1(X), fun(Y) -> bind(f2(Y), fun(Z) -> bind(f3(Z), continuation([])) end) end) end. %% fun(X) -> bind(f1(X), fun(Y) -> bind(f2(Y), fun(Z) -> bind(f3(Z), return()) end) end) end. %% fun(X) -> bind(f1(X), fun(Y) -> bind(f2(Y), fun(Z) -> bind(f3(Z), return()) end) end) end. %% fun(X) -> fun(f1(X)) -> bind(f2(f1(X)), fun(Z) -> bind(f3(Z), return()) end) end end. %% fun(X) -> bind(f2(f1(X)), fun(Z) -> bind(f3(Z), return()) end) end. %% fun(X) -> fun(f2(f1(X))) -> bind(f3(f2(f1(X))), return()) end end. %% fun(X) -> bind(f3(f2(f1(X))), return()) end. %% fun(X) -> bind(f3(f2(f1(X))), fun(A) -> A end) end. %% fun(X) -> fun(f3(f2(f1(X)))) -> f3(f2(f1(X))) end end. %% fun(X) -> f3(f2(f1(X))) end. %% f3(f2(f1(X))). continuation([]) -> return(); continuation([F | RF]) -> fun(A) -> bind(F(A), continuation(RF)) end. f_test() -> Cont = continuation([fun f1/1,fun f2/1,fun f3/1]), io:format(user, "*** Result = ~p~n~n", [Cont(0)]), io:format(user, "*** Result = ~p~n~n", [Cont(1)]), io:format(user, "*** Result = ~p~n~n", [Cont(2)]), io:format(user, "*** Result = ~p~n~n", [Cont(3)]). %% ==================================================================== %% Internal functions %% ==================================================================== f1(A) -> io:format(user, "f1 >>> ~p~n", [A]), case A of 3 -> nothing; _ -> A + 1 end. f2(A) -> io:format(user, "f2 >>> ~p~n", [A]), case A of 3 -> nothing; _ -> A + 1 end. f3(A) -> io:format(user, "f3 >>> ~p~n", [A]), case A of 3 -> nothing; _ -> A + 1 end.