From: Siraaj Khandkar Date: Fri, 15 Aug 2014 02:56:24 +0000 (-0400) Subject: Amortize list traversal. X-Git-Tag: 1.0.0~6 X-Git-Url: https://git.xandkar.net/?a=commitdiff_plain;h=17b5d686d1b6c608c8fab801f44701bb0c6cccf3;p=hope.git Amortize list traversal. --- diff --git a/src/hope_list.erl b/src/hope_list.erl index 5ce527c..9ce451e 100644 --- a/src/hope_list.erl +++ b/src/hope_list.erl @@ -16,11 +16,11 @@ -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)).