Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add lists:usort/2,3 #798

Merged
merged 1 commit into from
Sep 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ functions that default to `?ATOMVM_NVS_NS` are deprecated now).
- Added `unicode` module with `characters_to_list/1,2` and `characters_to_binary/1,2,3` functions
- Added support for `crypto:hash/2` (ESP32 and generic_unix with openssl)
- Added links to process_info/2
- Added lists:usort/1,2

### Fixed
- Fixed issue with formatting integers with io:format() on STM32 platform
Expand Down
49 changes: 49 additions & 0 deletions libs/estdlib/src/lists.erl
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
join/2,
seq/2, seq/3,
sort/1, sort/2,
usort/1, usort/2,
duplicate/2,
sublist/2
]).
Expand Down Expand Up @@ -464,6 +465,7 @@ seq(From, To, Incr, Accum) ->
%%
%% @end
%%-----------------------------------------------------------------------------
-spec sort(List :: [T]) -> [T].
sort(List) when is_list(List) ->
sort(fun lt/2, List).

Expand All @@ -475,6 +477,7 @@ sort(List) when is_list(List) ->
%%
%% @end
%%-----------------------------------------------------------------------------
-spec sort(Fun :: fun((T, T) -> boolean()), List :: [T]) -> [T].
sort(Fun, List) when is_function(Fun), is_list(List) ->
quick_sort(Fun, List).

Expand All @@ -490,6 +493,52 @@ quick_sort(_Fun, []) ->
%% @private
lt(A, B) -> A < B.

%%-----------------------------------------------------------------------------
%% @param List a list
%% @returns Sorted list with duplicates removed, ordered by `<'
%% @see sort/1
%% @doc Returns a unique, sorted list, using `<' operator to determine sort order.
%% @end
%%-----------------------------------------------------------------------------
-spec usort(List :: [T]) -> [T].
usort(List) ->
Sorted = sort(List),
unique(Sorted).

%%-----------------------------------------------------------------------------
%% @param Fun sort function
%% @param List a list
%% @returns Sorted list with duplicates removed, ordered by Fun.
%% @see sort/2
%% @doc Returns a unique, sorted list.
%% @end
%%-----------------------------------------------------------------------------
-spec usort(Fun :: fun((T, T) -> boolean()), List :: [T]) -> [T].
usort(Fun, List) ->
Sorted = sort(Fun, List),
unique(Sorted, Fun).

%% @private
unique(Sorted) ->
unique(Sorted, fun(X, Y) -> X =< Y end).

%% @private
unique(Sorted, Fun) ->
unique(Sorted, Fun, []).

%% @private
unique([], _Fun, []) ->
[];
unique([X], _Fun, Acc) ->
lists:reverse([X | Acc]);
unique([X, Y | Tail], Fun, Acc) ->
case Fun(X, Y) andalso Fun(Y, X) of
true ->
unique([Y | Tail], Fun, Acc);
false ->
unique([Y | Tail], Fun, [X | Acc])
end.

%%-----------------------------------------------------------------------------
%% @param Elem the element to duplicate
%% @param Count the number of times to duplicate the element
Expand Down
20 changes: 19 additions & 1 deletion tests/libs/estdlib/test_lists.erl
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ test() ->
ok = test_join(),
ok = test_seq(),
ok = test_sort(),
ok = test_usort(),
ok.

test_nth() ->
Expand Down Expand Up @@ -208,15 +209,32 @@ test_sort() ->
?ASSERT_MATCH(lists:sort([]), []),
?ASSERT_MATCH(lists:sort([1]), [1]),
?ASSERT_MATCH(lists:sort([1, 3, 5, 2, 4]), [1, 2, 3, 4, 5]),
?ASSERT_MATCH(lists:sort([1, 3, 5, 2, 3, 4]), [1, 2, 3, 3, 4, 5]),
?ASSERT_MATCH(lists:sort([c, a, b, d]), [a, b, c, d]),
?ASSERT_MATCH(lists:sort([#{}, z]), [z, #{}]),

?ASSERT_MATCH(lists:sort(fun(A, B) -> A > B end, [1, 2, 3, 4, 5]), [5, 4, 3, 2, 1]),
?ASSERT_MATCH(lists:sort(fun(A, B) -> A > B end, [1, 2, 3, 4, 3, 5]), [5, 4, 3, 3, 2, 1]),

?ASSERT_FAILURE(lists:sort(1), function_clause),
?ASSERT_FAILURE(lists:sort(fun(A, B) -> A > B end, 1), function_clause),
?ASSERT_FAILURE(lists:sort(1, [1]), function_clause),

ok.

test_usort() ->
?ASSERT_MATCH(lists:usort([]), []),
?ASSERT_MATCH(lists:usort([1]), [1]),
?ASSERT_MATCH(lists:usort([1, 3, 5, 2, 3, 4]), [1, 2, 3, 4, 5]),
?ASSERT_MATCH(lists:usort([1, 3, 5, 2, 1, 4]), [1, 2, 3, 4, 5]),
?ASSERT_MATCH(lists:usort([1, 3, 5, 2, 5, 4]), [1, 2, 3, 4, 5]),

?ASSERT_MATCH(lists:usort(fun(A, B) -> A > B end, [1, 2, 3, 4, 3, 5]), [5, 4, 3, 3, 2, 1]),
?ASSERT_MATCH(lists:usort(fun(A, B) -> A >= B end, [1, 2, 3, 4, 3, 5]), [5, 4, 3, 2, 1]),

?ASSERT_FAILURE(lists:usort(1), function_clause),
?ASSERT_FAILURE(lists:usort(fun(A, B) -> A > B end, 1), function_clause),
?ASSERT_FAILURE(lists:usort(1, [1]), function_clause),

ok.

id(X) -> X.