From: Siraaj Khandkar Date: Tue, 15 Jul 2014 01:08:20 +0000 (-0400) Subject: Add result:lift_exn/1 X-Git-Tag: 1.0.0~30 X-Git-Url: https://git.xandkar.net/?a=commitdiff_plain;h=8fc25ea1d79ab2c6148b73260490e804bd76ff7c;p=hope.git Add result:lift_exn/1 --- diff --git a/src/hope_result.erl b/src/hope_result.erl index 5d0ea7e..1b217f8 100644 --- a/src/hope_result.erl +++ b/src/hope_result.erl @@ -7,6 +7,7 @@ -export( [ pipe/2 + , lift_exn/1 ]). @@ -29,3 +30,19 @@ pipe([F|Fs], 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.