Implement hope_list:map_rev/2 1.5.0
authorSiraaj Khandkar <siraaj@khandkar.net>
Sun, 21 Dec 2014 08:10:06 +0000 (03:10 -0500)
committerSiraaj Khandkar <siraaj@khandkar.net>
Sun, 21 Dec 2014 08:10:06 +0000 (03:10 -0500)
O(N), tail-recursive equivalent to lists:rev(lists:map(F, L))

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

index 6ea0b7c..eb6f442 100644 (file)
@@ -1,7 +1,7 @@
 {application, hope,
  [
   {description, "Higher Order Programming in Erlang"},
-  {vsn, "1.4.0"},
+  {vsn, "1.5.0"},
   {registered, []},
   {applications, [
                   kernel,
index 9ce451e..b93ad2d 100644 (file)
@@ -6,6 +6,7 @@
 
 -export(
     [ unique_preserve_order/1
+    , map_rev/2
     ]).
 
 
     [A].
 
 
+%% @doc O(N), tail-recursive equivalent to lists:rev(lists:map(F, L))
+%% @end
+-spec map_rev([A], fun((A) -> (B))) ->
+    [B].
+map_rev(Xs, F) ->
+    map_rev_acc(Xs, F, []).
+
+-spec map_rev_acc([A], fun((A) -> (B)), [B]) ->
+    [B].
+map_rev_acc([], _, Ys) ->
+    Ys;
+map_rev_acc([X|Xs], F, Ys) ->
+    Y = F(X),
+    map_rev_acc(Xs, F, [Y|Ys]).
+
+
 -spec unique_preserve_order(t(A)) ->
     t(A).
 unique_preserve_order(L) ->
index fd2393c..f591e83 100644 (file)
@@ -12,6 +12,7 @@
 -export(
     [ t_unique_preserve_order/1
     , t_hope_list_specs/1
+    , t_map_rev/1
     ]).
 
 
@@ -31,6 +32,7 @@ groups() ->
     Tests =
         [ t_unique_preserve_order
         , t_hope_list_specs
+        , t_map_rev
         ],
     Properties = [parallel],
     [{?GROUP, Properties, Tests}].
@@ -40,6 +42,11 @@ groups() ->
 %%  Test cases
 %% =============================================================================
 
+t_map_rev(_Cfg) ->
+    F = fun (N) -> N + 1 end,
+    [4, 3, 2] = hope_list:map_rev([1, 2, 3], F),
+    []        = hope_list:map_rev([], F).
+
 t_unique_preserve_order(_Cfg) ->
     ?PROPTEST(prop_unique_preserve_order).
 
This page took 0.021193 seconds and 4 git commands to generate.