Amortize list traversal.
authorSiraaj Khandkar <siraaj@khandkar.net>
Fri, 15 Aug 2014 02:56:24 +0000 (22:56 -0400)
committerSiraaj Khandkar <siraaj@khandkar.net>
Fri, 15 Aug 2014 02:56:24 +0000 (22:56 -0400)
src/hope_list.erl

index 5ce527c..9ce451e 100644 (file)
 -spec unique_preserve_order(t(A)) ->
     t(A).
 unique_preserve_order(L) ->
-    AppendIfNew =
+    PrependIfNew =
         fun (X, Xs) ->
             case lists:member(X, Xs)
-            of  true  -> Xs
-            ;   false -> Xs ++ [X]
+            of  true  ->      Xs
+            ;   false -> [X | Xs]
             end
         end,
-    lists:foldl(AppendIfNew, [], L).
+    lists:reverse(lists:foldl(PrependIfNew, [], L)).
This page took 0.024322 seconds and 4 git commands to generate.