c461d04d1b70d3bbd292e954aa21c55eb4921c5e
[hope.git] / src / hope_option.erl
1 -module(hope_option).
2
3
4 -export_type(
5 [ t/1
6 ]).
7
8 -export(
9 [ put/2
10 , get/2
11 , map/2
12 ]).
13
14
15 -type t(A) ::
16 none
17 | {some, A}
18 .
19
20
21 -spec put(A, fun((A) -> boolean())) ->
22 t(A).
23 put(X, F) ->
24 case F(X)
25 of true -> {some, X}
26 ; false -> none
27 end.
28
29 -spec get(t(A), Default :: A) ->
30 A.
31 get({some, X}, _) -> X;
32 get(none , Y) -> Y.
33
34 -spec map(t(A), fun((A) -> (B))) ->
35 t(B).
36 map({some, X}, F) -> {some, F(X)};
37 map(none , _) -> none.
This page took 0.045786 seconds and 3 git commands to generate.