Implement (and test) hope_list:divide/2 3.8.0
authorSiraaj Khandkar <siraaj@khandkar.net>
Tue, 15 Sep 2015 20:49:27 +0000 (16:49 -0400)
committerSiraaj Khandkar <siraaj@khandkar.net>
Tue, 15 Sep 2015 20:49:27 +0000 (16:49 -0400)
Divides 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.

src/hope.app.src
src/hope_list.erl
test/hope_list_SUITE.erl

index c5c860f..91cd52f 100644 (file)
@@ -1,7 +1,7 @@
 {application, hope,
  [
   {description, "Higher Order Programming in Erlang"},
-  {vsn, "3.7.0"},
+  {vsn, "3.8.0"},
   {registered, []},
   {applications, [
                   kernel,
index 63d9c99..b5edf42 100644 (file)
     , map_slow/2
     , map_result/2  % Not tail-recursive
     , first_match/2
+    , divide/2
     ]).
 
-
 -define(DEFAULT_RECURSION_LIMIT, 1000).
 
-
 -type t(A) ::
     [A].
 
-
 %% @doc Tail-recursive equivalent of lists:map/2
 %% @end
 -spec map([A], fun((A) -> (B))) ->
@@ -74,7 +72,6 @@ map([X1, X2, X3, X4, X5 | Xs], F, RecursionLimit, RecursionCount) ->
 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
@@ -129,3 +126,28 @@ first_match([{Tag, F} | Tests], 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].
index e1f8375..386cdc6 100644 (file)
@@ -20,6 +20,7 @@
     , t_manual_map_result/1
     , t_manual_map_rev/1
     , t_manual_map_slow/1
+    , t_manual_divide/1
     ]).
 
 
@@ -49,6 +50,7 @@ groups() ->
         , t_manual_map_result
         , t_manual_map_rev
         , t_manual_map_slow
+        , t_manual_divide
         ],
     Properties = [parallel],
     [{?GROUP, Properties, Tests}].
@@ -84,6 +86,35 @@ t_manual_map_slow(_Cfg) ->
     [2, 3, 4] = hope_list:map_slow([1, 2, 3], F),
     []        = hope_list:map_slow([], F).
 
+t_manual_divide(_Cfg) ->
+    try
+        hope_list:divide([a, b, c], -1)
+    catch
+        error:hope_list__divide__size_must_be_a_positive_integer -> ok
+    end,
+    try
+        hope_list:divide([a, b, c], 0)
+    catch
+        error:hope_list__divide__size_must_be_a_positive_integer -> ok
+    end,
+    [[c], [b], [a]] = hope_list:divide([a, b, c], 1),
+    [[c], [b, a]]   = hope_list:divide([a, b, c], 2),
+    [[c, b, a]]     = hope_list:divide([a, b, c], 3),
+    [[c, b, a]]     = hope_list:divide([a, b, c], 4),
+    [[c, b, a]]     = hope_list:divide([a, b, c], 5),
+    try
+        hope_list:divide([], 0)
+    catch
+        error:hope_list__divide__size_must_be_a_positive_integer -> ok
+    end,
+    try
+        hope_list:divide([], -1)
+    catch
+        error:hope_list__divide__size_must_be_a_positive_integer -> ok
+    end,
+    [[f, e], [d,   c], [b, a]] = hope_list:divide([a, b, c, d, e, f], 2),
+    [[f, e,   d], [c,   b, a]] = hope_list:divide([a, b, c, d, e, f], 3).
+
 %% =============================================================================
 %%  Generated test cases
 %% =============================================================================
This page took 0.032838 seconds and 4 git commands to generate.