Implement (and test) hope_list:divide/2
[hope.git] / src / hope_list.erl
CommitLineData
a6244ba2
SK
1-module(hope_list).
2
3-export_type(
4 [ t/1
5 ]).
6
7-export(
8 [ unique_preserve_order/1
ff793acf 9 , map/2
781f182f 10 , map/3 % Tunable recursion limit
c66ddf80 11 , map_rev/2
2a81fbac 12 , map_slow/2
54ab0c82 13 , map_result/2 % Not tail-recursive
a626cf31 14 , first_match/2
fcfc097a 15 , divide/2
a6244ba2
SK
16 ]).
17
781f182f 18-define(DEFAULT_RECURSION_LIMIT, 1000).
ff793acf 19
a6244ba2
SK
20-type t(A) ::
21 [A].
22
ff793acf
SK
23%% @doc Tail-recursive equivalent of lists:map/2
24%% @end
25-spec map([A], fun((A) -> (B))) ->
26 [B].
27map(Xs, F) ->
781f182f 28 map(Xs, F, ?DEFAULT_RECURSION_LIMIT).
ff793acf 29
781f182f 30-spec map([A], fun((A) -> (B)), RecursionLimit :: non_neg_integer()) ->
ff793acf 31 [B].
781f182f
SK
32map(Xs, F, RecursionLimit) ->
33 map(Xs, F, RecursionLimit, 0).
34
35map([], _, _, _) ->
ff793acf 36 [];
781f182f 37map([X1], F, _, _) ->
ff793acf
SK
38 Y1 = F(X1),
39 [Y1];
781f182f 40map([X1, X2], F, _, _) ->
ff793acf
SK
41 Y1 = F(X1),
42 Y2 = F(X2),
43 [Y1, Y2];
781f182f 44map([X1, X2, X3], F, _, _) ->
ff793acf
SK
45 Y1 = F(X1),
46 Y2 = F(X2),
47 Y3 = F(X3),
48 [Y1, Y2, Y3];
781f182f 49map([X1, X2, X3, X4], F, _, _) ->
ff793acf
SK
50 Y1 = F(X1),
51 Y2 = F(X2),
52 Y3 = F(X3),
53 Y4 = F(X4),
54 [Y1, Y2, Y3, Y4];
781f182f 55map([X1, X2, X3, X4, X5 | Xs], F, RecursionLimit, RecursionCount) ->
ff793acf
SK
56 Y1 = F(X1),
57 Y2 = F(X2),
58 Y3 = F(X3),
59 Y4 = F(X4),
60 Y5 = F(X5),
61 Ys =
781f182f 62 case RecursionCount > RecursionLimit
ff793acf 63 of true -> map_slow(Xs, F)
781f182f 64 ; false -> map (Xs, F, RecursionLimit, RecursionCount + 1)
ff793acf
SK
65 end,
66 [Y1, Y2, Y3, Y4, Y5 | Ys].
67
2a81fbac
SK
68%% @doc lists:reverse(map_rev(L, F))
69%% @end
70-spec map_slow([A], fun((A) -> (B))) ->
71 [B].
72map_slow(Xs, F) ->
73 lists:reverse(map_rev(Xs, F)).
74
5e20a667
SK
75%% @doc Tail-recursive alternative to lists:map/2, which accumulates and
76%% returns list in reverse order.
c66ddf80
SK
77%% @end
78-spec map_rev([A], fun((A) -> (B))) ->
79 [B].
80map_rev(Xs, F) ->
81 map_rev_acc(Xs, F, []).
82
83-spec map_rev_acc([A], fun((A) -> (B)), [B]) ->
84 [B].
85map_rev_acc([], _, Ys) ->
86 Ys;
87map_rev_acc([X|Xs], F, Ys) ->
88 Y = F(X),
89 map_rev_acc(Xs, F, [Y|Ys]).
90
54ab0c82
SK
91-spec map_result([A], fun((A) -> (hope_result:t(B, C)))) ->
92 hope_result:t([B], C).
93map_result([], _) ->
94 {ok, []};
95map_result([X | Xs], F) ->
96 case F(X)
97 of {ok, Y} ->
98 case map_result(Xs, F)
99 of {ok, Ys} ->
100 {ok, [Y | Ys]}
101 ; {error, _}=Error ->
102 Error
103 end
104 ; {error, _}=Error ->
105 Error
106 end.
c66ddf80 107
a6244ba2
SK
108-spec unique_preserve_order(t(A)) ->
109 t(A).
110unique_preserve_order(L) ->
17b5d686 111 PrependIfNew =
a6244ba2
SK
112 fun (X, Xs) ->
113 case lists:member(X, Xs)
17b5d686
SK
114 of true -> Xs
115 ; false -> [X | Xs]
a6244ba2
SK
116 end
117 end,
17b5d686 118 lists:reverse(lists:foldl(PrependIfNew, [], L)).
a626cf31
SK
119
120-spec first_match([{Tag, fun((A) -> boolean())}], A) ->
121 hope_option:t(Tag).
122first_match([], _) ->
123 none;
124first_match([{Tag, F} | Tests], X) ->
125 case F(X)
126 of true -> {some, Tag}
127 ; false -> first_match(Tests, X)
128 end.
fcfc097a
SK
129
130%% @doc Divide list into sublists of up to a requested size + a remainder.
131%% Order unspecified. Size < 1 raises an error:
132%% `hope_list__divide__size_must_be_a_positive_integer'
133%% @end
134-spec divide([A], pos_integer()) ->
135 [[A]].
136divide(_, Size) when Size < 1 orelse not is_integer(Size) ->
137 % Q: Why?
138 % A: For N < 0, what does it mean to have a negative-sized chunk?
139 % For N = 0, we can imagine that a single chunk is an empty list, but,
140 % how many such chunks should we produce?
141 % This is pretty-much equivalnet to the problem of deviding something by 0.
142 error(hope_list__divide__size_must_be_a_positive_integer);
143divide([], _) ->
144 [];
145divide([X1 | Xs], MaxChunkSize) ->
146 MoveIntoChunks =
147 fun (X2, {Chunk, Chunks, ChunkSize}) when ChunkSize >= MaxChunkSize ->
148 {[X2], [Chunk | Chunks], 1}
149 ; (X2, {Chunk, Chunks, ChunkSize}) ->
150 {[X2 | Chunk], Chunks, ChunkSize + 1}
151 end,
152 {Chunk, Chunks, _} = lists:foldl(MoveIntoChunks, {[X1], [], 1}, Xs),
153 [Chunk | Chunks].
This page took 0.035247 seconds and 4 git commands to generate.