Skip to content

Instantly share code, notes, and snippets.

Created Nov 19, 2012
What would you like to do?
-spec lift(fun()) -> maybe(_, _).
%% @doc Lift F into maybe().
lift(F) ->
try F() of
ok -> {ok, ok};
{ok, Res} -> {ok, Res};
error -> {error, error};
{error, Rsn} -> {error, Rsn};
Res -> {ok, Res}
throw:{error,Rsn} -> {error, Rsn};
_:{aborted, Rsn} -> exit({aborted, Rsn}); %Mnesia hack
_:Exn -> {error, {lifted_exn, Exn, erlang:get_stacktrace()}}
-spec unlift(fun(() -> broken())) -> _ | no_return().
%% @doc Call F and untag the result. Errors become exceptions.
unlift(F) ->
case F() of
ok -> ok;
{ok, Res} -> Res;
error -> throw({error, error});
{error, Rsn} -> throw({error, Rsn});
Res -> Res
%% @doc XXX this will be renamed
maybe(Thunk, Ok, Error) ->
maybe([Thunk, Error, Ok]).
maybe(CPS) ->
maybe(CPS, []).
maybe([Thunk, Error|Rest], Acc) ->
case lift(Thunk()) of
{error, Rsn} -> Error(Rsn);
{ok, Res} -> maybe(Rest, [Res|Acc])
maybe([Ok], Acc) -> apply(Ok, lists:reverse(Acc)).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment