-module(hope_option).
+-behavior(hope_monad).
-export_type(
[ t/1
-export(
[ put/2
, get/2
+ , return/1
, map/2
, iter/2
+ , pipe/2
, of_result/1
]).
get({some, X}, _) -> X;
get(none , Y) -> Y.
+-spec return(A) ->
+ {some, A}.
+return(X) ->
+ {some, X}.
+
-spec map(t(A), fun((A) -> (B))) ->
t(B).
map({some, X}, F) -> {some, F(X)};
iter({some, X}, F) -> ok = F(X);
iter(none , _) -> ok.
+-spec pipe([fun((A) -> t(B))], A) ->
+ t(B).
+pipe([], X) ->
+ return(X);
+pipe([F|Fs], X) ->
+ case F(X)
+ of none -> none
+ ; {some, Y} -> pipe(Fs, Y)
+ end.
+
-spec of_result(hope_result:t(A, _B)) ->
t(A).
of_result({ok, X}) -> {some, X};
, t_get/1
, t_map/1
, t_iter/1
+ , t_pipe/1
]).
, t_get
, t_map
, t_iter
+ , t_pipe
],
Properties = [parallel],
[ {?GROUP, Properties, Tests}
ResultError = {error, Bar},
{some, Foo} = hope_option:of_result(ResultOk),
none = hope_option:of_result(ResultError).
+
+t_pipe(_Cfg) ->
+ Steps =
+ [ fun (0) -> hope_option:return(1); (_) -> none end
+ , fun (1) -> hope_option:return(2); (_) -> none end
+ , fun (2) -> hope_option:return(3); (_) -> none end
+ ],
+ {some, 3} = hope_option:pipe(Steps, 0),
+ none = hope_option:pipe(Steps, 1),
+ none = hope_option:pipe(Steps, 2),
+ none = hope_option:pipe(Steps, 3).
, t_pipe_error/1
, t_hope_result_specs/1
, t_lift_exn/1
+ , t_return/1
+ , t_map/1
]).
-define(GROUP_PIPE, result_pipe).
-define(GROUP_SPEC, result_spec).
-define(GROUP_LIFT, result_lift_exn).
+-define(GROUP_OTHER, result_other).
%% ============================================================================
[ {group, ?GROUP_PIPE}
, {group, ?GROUP_SPEC}
, {group, ?GROUP_LIFT}
+ , {group, ?GROUP_OTHER}
].
groups() ->
LiftTests =
[ t_lift_exn
],
+ OtherTests =
+ [ t_return
+ , t_map
+ ],
Properties = [parallel],
[ {?GROUP_PIPE, Properties, PipeTests}
, {?GROUP_SPEC, Properties, SpecTests}
, {?GROUP_LIFT, Properties, LiftTests}
+ , {?GROUP_OTHER, Properties, OtherTests}
].
-init_per_group(?GROUP_LIFT, Cfg) ->
- Cfg;
-init_per_group(?GROUP_SPEC, Cfg) ->
- Cfg;
init_per_group(?GROUP_PIPE, Cfg) ->
Steps =
[ fun (0) -> {ok, 1}; (X) -> {error, X} end
, fun (1) -> {ok, 2}; (X) -> {error, X} end
, fun (2) -> {ok, 3}; (X) -> {error, X} end
],
- hope_kv_list:set(Cfg, steps, Steps).
+ hope_kv_list:set(Cfg, steps, Steps);
+init_per_group(_, Cfg) ->
+ Cfg.
-end_per_group(?GROUP_LIFT, _Cfg) ->
- ok;
-end_per_group(?GROUP_SPEC, _Cfg) ->
- ok;
-end_per_group(?GROUP_PIPE, _Cfg) ->
+end_per_group(_, _Cfg) ->
ok.
H = hope_result:lift_exn(F, Label),
{error, {Class, Reason}} = G(ok),
{error, {Label, {Class, Reason}}} = H(ok).
+
+t_return(_Cfg) ->
+ X = foo,
+ {ok, X} = hope_result:return(X).
+
+t_map(_Cfg) ->
+ X = foo,
+ Y = bar,
+ F = fun (foo) -> Y end,
+ {ok, Y} = hope_result:map({ok, X}, F),
+ {error, X} = hope_result:map({error, X}, F).