Add result:lift_exn/1
authorSiraaj Khandkar <siraaj@khandkar.net>
Tue, 15 Jul 2014 01:08:20 +0000 (21:08 -0400)
committerSiraaj Khandkar <siraaj@khandkar.net>
Tue, 15 Jul 2014 01:08:20 +0000 (21:08 -0400)
src/hope_result.erl

index 5d0ea7e..1b217f8 100644 (file)
@@ -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.
This page took 0.032183 seconds and 4 git commands to generate.