X-Git-Url: https://git.xandkar.net/?a=blobdiff_plain;f=src%2Fhope_result.erl;h=44f3ddd58b92e21ba0941e4d274b31d495fd68c9;hb=70cf8e86a06d8721fcc8b658bb2be1f7b401326f;hp=06f1eebd43f279d75d7f309fb55e77664b5a566c;hpb=3b156801097bcd07272f1db3a62a675495fa9d77;p=hope.git diff --git a/src/hope_result.erl b/src/hope_result.erl index 06f1eeb..44f3ddd 100644 --- a/src/hope_result.erl +++ b/src/hope_result.erl @@ -1,11 +1,13 @@ -module(hope_result). + -export_type( [ t/2 ]). -export( [ pipe/2 + , lift_exn/1 ]). @@ -15,9 +17,33 @@ . -pipe([] , X) -> X; +-spec pipe([F], X) -> + t(Ok, Error) + when X :: any() + , Ok :: any() + , Error :: any() + , F :: fun((X) -> t(Ok, Error)) + . +pipe([], X) -> + {ok, X}; pipe([F|Fs], X) -> case F(X) of {error, _}=E -> E ; {ok, Y} -> pipe(Fs, Y) end. + +-spec lift_exn(F) -> G + when F :: fun((A)-> B) + , G :: fun((A)-> t(B, {Class, Reason :: any()})) + , Class :: error + | exit + | throw + . +lift_exn(F) when is_function(F, 1) -> + fun(X) -> + try + {ok, F(X)} + catch Class:Reason -> + {error, {Class, Reason}} + end + end.