Add labeled result lifting.
[hope.git] / src / hope_result.erl
index 1b217f8..feda537 100644 (file)
@@ -8,6 +8,7 @@
 -export(
     [ pipe/2
     , lift_exn/1
+    , lift_exn/2
     ]).
 
 
@@ -24,7 +25,8 @@
        , Error :: any()
        , F     :: fun((X) -> t(Ok, Error))
        .
-pipe([]    , X) -> X;
+pipe([], X) ->
+    {ok, X};
 pipe([F|Fs], X) ->
     case F(X)
     of  {error, _}=E -> E
@@ -46,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.
This page took 0.022194 seconds and 4 git commands to generate.