Skip to content

Commit

Permalink
Add lists:append/1.
Browse files Browse the repository at this point in the history
These changes are made under both the "Apache 2.0" and the "GNU Lesser General Public License 2.1 or later" license terms (dual license).

SPDX-License-Identifier: Apache-2.0 OR LGPL-2.1-or-later

Signed-off-by: migmatore <[email protected]>
  • Loading branch information
migmatore committed Feb 17, 2025
1 parent 7caa566 commit ef8cabb
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Support to OTP-28
- Added support for `ets:update_counter/3` and `ets:update_counter/4`.
- Added `erlang:+/1`
- Added `lists:append/1`

### Fixed
- ESP32: improved sntp sync speed from a cold boot.
Expand Down
21 changes: 20 additions & 1 deletion libs/estdlib/src/lists.erl
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
% Copyright 2017-2023 Fred Dushin <[email protected]>
% split/2 function Copyright Ericsson AB 1996-2023.
% keytake/3 function Copyright Ericsson AB 1996-2024.
% append/1 function Copyright Ericsson AB 1996-2025.
%
% Licensed under the Apache License, Version 2.0 (the "License");
% you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -60,7 +61,8 @@
split/2,
usort/1, usort/2,
duplicate/2,
sublist/2
sublist/2,
append/1
]).

%%-----------------------------------------------------------------------------
Expand Down Expand Up @@ -758,3 +760,20 @@ sublist(List, Len) when is_integer(Len) andalso Len >= 0 ->
sublist0([], _Len) -> [];
sublist0(_, 0) -> [];
sublist0([H | Tail], Len) -> [H | sublist0(Tail, Len - 1)].

%%-----------------------------------------------------------------------------
%% @param ListOfLists a list of lists to make the general list from
%% @returns a list made of the sublists of `ListOfLists`.
%% @doc Returns a list in which all the sublists of `ListOfLists` have been appended.
%% @end
%%-----------------------------------------------------------------------------
%% Attribution: https://github.com/erlang/otp/blob/35037ba900e15bd98e778e567079b426531d0085/lib/stdlib/src/lists.erl#L202
-spec append(ListOfLists) -> List1 when
ListOfLists :: [List],
List :: [T],
List1 :: [T],
T :: term().

append([E]) -> E;
append([H | T]) -> H ++ append(T);
append([]) -> [].
12 changes: 12 additions & 0 deletions tests/libs/estdlib/test_lists.erl
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
% This file is part of AtomVM.
%
% Copyright 2019-2021 Fred Dushin <[email protected]>
% Copyright 2025 migmatore <[email protected]>
%
% Licensed under the Apache License, Version 2.0 (the "License");
% you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -49,6 +50,7 @@ test() ->
ok = test_filtermap(),
ok = test_last(),
ok = test_mapfoldl(),
ok = test_append(),
ok.

test_nth() ->
Expand Down Expand Up @@ -321,4 +323,14 @@ test_mapfoldl() ->
?ASSERT_ERROR(lists:mapfoldl(fun(X, A) -> {X * A, A + 1} end, 1, foo), function_clause),
ok.

test_append() ->
?ASSERT_MATCH(lists:append([]), []),
?ASSERT_MATCH(lists:append([[1, 2, 3, 4]]), [1, 2, 3, 4]),
?ASSERT_MATCH(lists:append([[1, 2], [3, 4]]), [1, 2, 3, 4]),
?ASSERT_MATCH(lists:append([[1, 2], [a, b]]), [1, 2, a, b]),
?ASSERT_MATCH(lists:append([["1", "2"], [a, b]]), [1, 2, a, b]),
?ASSERT_ERROR(lists:append(1), function_clause),
?ASSERT_ERROR(lists:append(1, 3), function_clause),
ok.

id(X) -> X.

0 comments on commit ef8cabb

Please sign in to comment.