From: Siraaj Khandkar Date: Thu, 11 Dec 2014 21:04:17 +0000 (-0500) Subject: Add labeled result lifting. X-Git-Tag: 1.1.0 X-Git-Url: https://git.xandkar.net/?a=commitdiff_plain;h=b79afea06befb5309f4a3f0388e587e17c484b83;p=hope.git Add labeled result lifting. --- diff --git a/src/hope.app.src b/src/hope.app.src index bf3fd3d..3b019ef 100644 --- a/src/hope.app.src +++ b/src/hope.app.src @@ -1,7 +1,7 @@ {application, hope, [ {description, "Higher Order Programming in Erlang"}, - {vsn, "0.0.0"}, + {vsn, "1.1.0"}, {registered, []}, {applications, [ kernel, diff --git a/src/hope_result.erl b/src/hope_result.erl index 44f3ddd..feda537 100644 --- a/src/hope_result.erl +++ b/src/hope_result.erl @@ -8,6 +8,7 @@ -export( [ pipe/2 , lift_exn/1 + , lift_exn/2 ]). @@ -47,3 +48,19 @@ lift_exn(F) when is_function(F, 1) -> {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. diff --git a/test/hope_result_SUITE.erl b/test/hope_result_SUITE.erl index 9587345..390f28c 100644 --- a/test/hope_result_SUITE.erl +++ b/test/hope_result_SUITE.erl @@ -90,6 +90,9 @@ t_hope_result_specs(_) -> t_lift_exn(_Cfg) -> Class = throw, Reason = foofoo, + Label = bar, F = fun (ok) -> apply(erlang, Class, [Reason]) end, G = hope_result:lift_exn(F), - {error, {Class, Reason}} = G(ok). + H = hope_result:lift_exn(F, Label), + {error, {Class, Reason}} = G(ok), + {error, {Label, {Class, Reason}}} = H(ok).