X-Git-Url: https://git.xandkar.net/?a=blobdiff_plain;f=src%2Fhope_list.erl;h=b5edf42cee87533dc8d630334a175c78b7326756;hb=797ff79bcd48da49fcb82b00013224c0831203a4;hp=91ab9169db829d617c9c967ec398f8137923e16e;hpb=ff793acf03456b6bd4c12ea330a9311f7e94cc77;p=hope.git diff --git a/src/hope_list.erl b/src/hope_list.erl index 91ab916..b5edf42 100644 --- a/src/hope_list.erl +++ b/src/hope_list.erl @@ -7,61 +7,64 @@ -export( [ unique_preserve_order/1 , map/2 + , map/3 % Tunable recursion limit , map_rev/2 , map_slow/2 + , map_result/2 % Not tail-recursive + , first_match/2 + , divide/2 ]). - --define(RECURSION_LIMIT, 1000). - +-define(DEFAULT_RECURSION_LIMIT, 1000). -type t(A) :: [A]. - %% @doc Tail-recursive equivalent of lists:map/2 %% @end -spec map([A], fun((A) -> (B))) -> [B]. map(Xs, F) -> - map(Xs, F, 0). + map(Xs, F, ?DEFAULT_RECURSION_LIMIT). --spec map([A], fun((A) -> (B)), non_neg_integer()) -> +-spec map([A], fun((A) -> (B)), RecursionLimit :: non_neg_integer()) -> [B]. -map([], _, _) -> +map(Xs, F, RecursionLimit) -> + map(Xs, F, RecursionLimit, 0). + +map([], _, _, _) -> []; -map([X1], F, _) -> +map([X1], F, _, _) -> Y1 = F(X1), [Y1]; -map([X1, X2], F, _) -> +map([X1, X2], F, _, _) -> Y1 = F(X1), Y2 = F(X2), [Y1, Y2]; -map([X1, X2, X3], F, _) -> +map([X1, X2, X3], F, _, _) -> Y1 = F(X1), Y2 = F(X2), Y3 = F(X3), [Y1, Y2, Y3]; -map([X1, X2, X3, X4], F, _) -> +map([X1, X2, X3, X4], F, _, _) -> Y1 = F(X1), Y2 = F(X2), Y3 = F(X3), Y4 = F(X4), [Y1, Y2, Y3, Y4]; -map([X1, X2, X3, X4, X5 | Xs], F, Count) -> +map([X1, X2, X3, X4, X5 | Xs], F, RecursionLimit, RecursionCount) -> Y1 = F(X1), Y2 = F(X2), Y3 = F(X3), Y4 = F(X4), Y5 = F(X5), Ys = - case Count > ?RECURSION_LIMIT + case RecursionCount > RecursionLimit of true -> map_slow(Xs, F) - ; false -> map (Xs, F, Count + 1) + ; false -> map (Xs, F, RecursionLimit, RecursionCount + 1) end, [Y1, Y2, Y3, Y4, Y5 | Ys]. - %% @doc lists:reverse(map_rev(L, F)) %% @end -spec map_slow([A], fun((A) -> (B))) -> @@ -69,7 +72,6 @@ map([X1, X2, X3, X4, X5 | Xs], F, Count) -> map_slow(Xs, F) -> lists:reverse(map_rev(Xs, F)). - %% @doc Tail-recursive alternative to lists:map/2, which accumulates and %% returns list in reverse order. %% @end @@ -86,6 +88,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). @@ -98,3 +116,38 @@ 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. + +%% @doc Divide list into sublists of up to a requested size + a remainder. +%% Order unspecified. Size < 1 raises an error: +%% `hope_list__divide__size_must_be_a_positive_integer' +%% @end +-spec divide([A], pos_integer()) -> + [[A]]. +divide(_, Size) when Size < 1 orelse not is_integer(Size) -> + % Q: Why? + % A: For N < 0, what does it mean to have a negative-sized chunk? + % For N = 0, we can imagine that a single chunk is an empty list, but, + % how many such chunks should we produce? + % This is pretty-much equivalnet to the problem of deviding something by 0. + error(hope_list__divide__size_must_be_a_positive_integer); +divide([], _) -> + []; +divide([X1 | Xs], MaxChunkSize) -> + MoveIntoChunks = + fun (X2, {Chunk, Chunks, ChunkSize}) when ChunkSize >= MaxChunkSize -> + {[X2], [Chunk | Chunks], 1} + ; (X2, {Chunk, Chunks, ChunkSize}) -> + {[X2 | Chunk], Chunks, ChunkSize + 1} + end, + {Chunk, Chunks, _} = lists:foldl(MoveIntoChunks, {[X1], [], 1}, Xs), + [Chunk | Chunks].