Add result:lift_exn/1
[hope.git] / src / hope_result.erl
CommitLineData
6731749b
SK
1-module(hope_result).
2
2a40de4f 3
3b156801
SK
4-export_type(
5 [ t/2
6 ]).
6731749b 7
3b156801
SK
8-export(
9 [ pipe/2
8fc25ea1 10 , lift_exn/1
3b156801 11 ]).
6731749b
SK
12
13
3b156801
SK
14-type t(A, B) ::
15 {ok, A}
16 | {error, B}
17 .
6731749b
SK
18
19
ed9905af
SK
20-spec pipe([F], X) ->
21 t(Ok, Error)
22 when X :: any()
23 , Ok :: any()
24 , Error :: any()
25 , F :: fun((X) -> t(Ok, Error))
26 .
6731749b
SK
27pipe([] , X) -> X;
28pipe([F|Fs], X) ->
29 case F(X)
30 of {error, _}=E -> E
31 ; {ok, Y} -> pipe(Fs, Y)
32 end.
8fc25ea1
SK
33
34-spec lift_exn(F) -> G
35 when F :: fun((A)-> B)
36 , G :: fun((A)-> t(B, {Class, Reason :: any()}))
37 , Class :: error
38 | exit
39 | throw
40 .
41lift_exn(F) when is_function(F, 1) ->
42 fun(X) ->
43 try
44 {ok, F(X)}
45 catch Class:Reason ->
46 {error, {Class, Reason}}
47 end
48 end.
This page took 0.021798 seconds and 4 git commands to generate.