X-Git-Url: https://git.xandkar.net/?a=blobdiff_plain;f=src%2Fhope_list.erl;h=63d9c990c389e5b2e22efb6835874f07f36f4c9f;hb=2fa2f0266468d1b726aae16141671fed267eab53;hp=abfb3d39ca74aa61f38a360ef502b5e1a2f7d6a9;hpb=781f182fe399d95fc144ba3b47112e803ba6af6e;p=hope.git diff --git a/src/hope_list.erl b/src/hope_list.erl index abfb3d3..63d9c99 100644 --- a/src/hope_list.erl +++ b/src/hope_list.erl @@ -10,6 +10,8 @@ , map/3 % Tunable recursion limit , map_rev/2 , map_slow/2 + , map_result/2 % Not tail-recursive + , first_match/2 ]). @@ -65,7 +67,6 @@ map([X1, X2, X3, X4, X5 | Xs], F, RecursionLimit, RecursionCount) -> end, [Y1, Y2, Y3, Y4, Y5 | Ys]. - %% @doc lists:reverse(map_rev(L, F)) %% @end -spec map_slow([A], fun((A) -> (B))) -> @@ -90,6 +91,22 @@ map_rev_acc([X|Xs], F, Ys) -> 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). @@ -102,3 +119,13 @@ unique_preserve_order(L) -> end end, lists:reverse(lists:foldl(PrependIfNew, [], L)). + +-spec first_match([{Tag, fun((A) -> boolean())}], A) -> + hope_option:t(Tag). +first_match([], _) -> + none; +first_match([{Tag, F} | Tests], X) -> + case F(X) + of true -> {some, Tag} + ; false -> first_match(Tests, X) + end.