, map/3 % Tunable recursion limit
, map_rev/2
, map_slow/2
+ , map_result/2 % Not tail-recursive
, first_match/2
]).
end,
[Y1, Y2, Y3, Y4, Y5 | Ys].
-
%% @doc lists:reverse(map_rev(L, F))
%% @end
-spec map_slow([A], fun((A) -> (B))) ->
Y = F(X),
map_rev_acc(Xs, F, [Y|Ys]).
+-spec map_result([A], fun((A) -> (hope_result:t(B, C)))) ->
+ hope_result:t([B], C).
+map_result([], _) ->
+ {ok, []};
+map_result([X | Xs], F) ->
+ case F(X)
+ of {ok, Y} ->
+ case map_result(Xs, F)
+ of {ok, Ys} ->
+ {ok, [Y | Ys]}
+ ; {error, _}=Error ->
+ Error
+ end
+ ; {error, _}=Error ->
+ Error
+ end.
-spec unique_preserve_order(t(A)) ->
t(A).
, t_map_slow/1
, t_map/1
, t_map_3/1
+ , t_map_result/1
]).
, t_map_slow
, t_map
, t_map_3
+ , t_map_result
],
Properties = [parallel],
[{?GROUP, Properties, Tests}].
t_hope_list_specs(_) ->
[] = proper:check_specs(hope_list).
+
+t_map_result(_Cfg) ->
+ AssertPositive =
+ fun (I) when I > 0 -> {ok, I}; (_) -> {error, negative} end,
+ AllPositives = lists:seq(1, 5),
+ AllNegatives = lists:seq(-5, -1),
+ Mixed = lists:seq(-5, 5),
+ {ok, AllPositives} = hope_list:map_result(AllPositives, AssertPositive),
+ {error, negative} = hope_list:map_result(AllNegatives, AssertPositive),
+ {error, negative} = hope_list:map_result(Mixed, AssertPositive).