Skip to content

Commit

Permalink
Merge pull request #1548 from migmatore/add_lists_append
Browse files Browse the repository at this point in the history
Add lists:append/1 and lists:append/2

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
  • Loading branch information
bettio committed Feb 18, 2025
2 parents 7caa566 + a749dc6 commit 8d71042
Show file tree
Hide file tree
Showing 3 changed files with 52 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` and `lists:append/2`

### Fixed
- ESP32: improved sntp sync speed from a cold boot.
Expand Down
38 changes: 37 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, append/2 functions 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,9 @@
split/2,
usort/1, usort/2,
duplicate/2,
sublist/2
sublist/2,
append/1,
append/2
]).

%%-----------------------------------------------------------------------------
Expand Down Expand Up @@ -758,3 +761,36 @@ 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/34f92c2a9cbb37ef22aecf6b95613d210509015a/lib/stdlib/src/lists.erl#L222
-spec append(ListOfLists) -> List1 when
ListOfLists :: [List],
List :: [T],
List1 :: [T],
T :: term().

append([E]) -> E;
append([H | T]) -> H ++ append(T);
append([]) -> [].

%%-----------------------------------------------------------------------------
%% @param List1 a list
%% @param List2 a list
%% @returns a list made of the elements of `List1' and `List2'.
%% @doc Returns a new list `List3', which is made from the elements of `List1' followed by the elements of `List2'.
%% @end
%%-----------------------------------------------------------------------------
%% Attribution: https://github.com/erlang/otp/blob/35037ba900e15bd98e778e567079b426531d0085/lib/stdlib/src/lists.erl#L202
-spec append(List1, List2) -> List3 when
List1 :: [T],
List2 :: [T],
List3 :: [T],
T :: term().

append(L1, L2) -> L1 ++ L2.
14 changes: 14 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,16 @@ 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_MATCH(lists:append("abc", "def"), "abcdef"),
?ASSERT_MATCH(lists:append([1, 2], [3, 4]), [1, 2, 3, 4]),
?ASSERT_ERROR(lists:append(1, 3), badarg),
ok.

id(X) -> X.

0 comments on commit 8d71042

Please sign in to comment.