From 8fc25ea1d79ab2c6148b73260490e804bd76ff7c Mon Sep 17 00:00:00 2001 From: Siraaj Khandkar Date: Mon, 14 Jul 2014 21:08:20 -0400 Subject: [PATCH] Add result:lift_exn/1 --- src/hope_result.erl | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) 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. -- 2.20.1