X-Git-Url: https://git.xandkar.net/?a=blobdiff_plain;f=src%2Fhope_result.erl;h=feda5379c1e6ec36e506530547937928b79e82a9;hb=b79afea06befb5309f4a3f0388e587e17c484b83;hp=a40629a1c52b58629b7a169dc8847c3f93bf83d0;hpb=2a40de4f9404d67967ad486d7d10b354791105fe;p=hope.git diff --git a/src/hope_result.erl b/src/hope_result.erl index a40629a..feda537 100644 --- a/src/hope_result.erl +++ b/src/hope_result.erl @@ -7,6 +7,8 @@ -export( [ pipe/2 + , lift_exn/1 + , lift_exn/2 ]). @@ -16,9 +18,49 @@ . -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. + +-spec lift_exn(F, Label) -> G + when F :: fun((A)-> B) + , G :: fun((A)-> t(B, {Label, {Class, Reason :: any()}})) + , Class :: error + | exit + | throw + . +lift_exn(F, Label) when is_function(F, 1) -> + fun(X) -> + try + {ok, F(X)} + catch Class:Reason -> + {error, {Label, {Class, Reason}}} + end + end.