1b217f845b4fa4c7cfff2a07cc272c9be3e7f762
[hope.git] / src / hope_result.erl
1 -module(hope_result).
2
3
4 -export_type(
5 [ t/2
6 ]).
7
8 -export(
9 [ pipe/2
10 , lift_exn/1
11 ]).
12
13
14 -type t(A, B) ::
15 {ok, A}
16 | {error, B}
17 .
18
19
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 .
27 pipe([] , X) -> X;
28 pipe([F|Fs], X) ->
29 case F(X)
30 of {error, _}=E -> E
31 ; {ok, Y} -> pipe(Fs, Y)
32 end.
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 .
41 lift_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.058964 seconds and 3 git commands to generate.