Skip to content

Commit 8d71042

Browse files
committed
Merge pull request #1548 from migmatore/add_lists_append
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
2 parents 7caa566 + a749dc6 commit 8d71042

File tree

3 files changed

+52
-1
lines changed

3 files changed

+52
-1
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
2121
- Support to OTP-28
2222
- Added support for `ets:update_counter/3` and `ets:update_counter/4`.
2323
- Added `erlang:+/1`
24+
- Added `lists:append/1` and `lists:append/2`
2425

2526
### Fixed
2627
- ESP32: improved sntp sync speed from a cold boot.

libs/estdlib/src/lists.erl

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
% Copyright 2017-2023 Fred Dushin <fred@dushin.net>
55
% split/2 function Copyright Ericsson AB 1996-2023.
66
% keytake/3 function Copyright Ericsson AB 1996-2024.
7+
% append/1, append/2 functions Copyright Ericsson AB 1996-2025.
78
%
89
% Licensed under the Apache License, Version 2.0 (the "License");
910
% you may not use this file except in compliance with the License.
@@ -60,7 +61,9 @@
6061
split/2,
6162
usort/1, usort/2,
6263
duplicate/2,
63-
sublist/2
64+
sublist/2,
65+
append/1,
66+
append/2
6467
]).
6568

6669
%%-----------------------------------------------------------------------------
@@ -758,3 +761,36 @@ sublist(List, Len) when is_integer(Len) andalso Len >= 0 ->
758761
sublist0([], _Len) -> [];
759762
sublist0(_, 0) -> [];
760763
sublist0([H | Tail], Len) -> [H | sublist0(Tail, Len - 1)].
764+
765+
%%-----------------------------------------------------------------------------
766+
%% @param ListOfLists a list of lists to make the general list from
767+
%% @returns a list made of the sublists of `ListOfLists'.
768+
%% @doc Returns a list in which all the sublists of `ListOfLists' have been appended.
769+
%% @end
770+
%%-----------------------------------------------------------------------------
771+
%% Attribution: https://github.com/erlang/otp/blob/34f92c2a9cbb37ef22aecf6b95613d210509015a/lib/stdlib/src/lists.erl#L222
772+
-spec append(ListOfLists) -> List1 when
773+
ListOfLists :: [List],
774+
List :: [T],
775+
List1 :: [T],
776+
T :: term().
777+
778+
append([E]) -> E;
779+
append([H | T]) -> H ++ append(T);
780+
append([]) -> [].
781+
782+
%%-----------------------------------------------------------------------------
783+
%% @param List1 a list
784+
%% @param List2 a list
785+
%% @returns a list made of the elements of `List1' and `List2'.
786+
%% @doc Returns a new list `List3', which is made from the elements of `List1' followed by the elements of `List2'.
787+
%% @end
788+
%%-----------------------------------------------------------------------------
789+
%% Attribution: https://github.com/erlang/otp/blob/35037ba900e15bd98e778e567079b426531d0085/lib/stdlib/src/lists.erl#L202
790+
-spec append(List1, List2) -> List3 when
791+
List1 :: [T],
792+
List2 :: [T],
793+
List3 :: [T],
794+
T :: term().
795+
796+
append(L1, L2) -> L1 ++ L2.

tests/libs/estdlib/test_lists.erl

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
% This file is part of AtomVM.
33
%
44
% Copyright 2019-2021 Fred Dushin <fred@dushin.net>
5+
% Copyright 2025 migmatore <kazakvova201@gmail.com>
56
%
67
% Licensed under the Apache License, Version 2.0 (the "License");
78
% you may not use this file except in compliance with the License.
@@ -49,6 +50,7 @@ test() ->
4950
ok = test_filtermap(),
5051
ok = test_last(),
5152
ok = test_mapfoldl(),
53+
ok = test_append(),
5254
ok.
5355

5456
test_nth() ->
@@ -321,4 +323,16 @@ test_mapfoldl() ->
321323
?ASSERT_ERROR(lists:mapfoldl(fun(X, A) -> {X * A, A + 1} end, 1, foo), function_clause),
322324
ok.
323325

326+
test_append() ->
327+
?ASSERT_MATCH(lists:append([]), []),
328+
?ASSERT_MATCH(lists:append([[1, 2, 3, 4]]), [1, 2, 3, 4]),
329+
?ASSERT_MATCH(lists:append([[1, 2], [3, 4]]), [1, 2, 3, 4]),
330+
?ASSERT_MATCH(lists:append([[1, 2], [a, b]]), [1, 2, a, b]),
331+
?ASSERT_MATCH(lists:append([["1", "2"], [a, b]]), ["1", "2", a, b]),
332+
?ASSERT_ERROR(lists:append(1), function_clause),
333+
?ASSERT_MATCH(lists:append("abc", "def"), "abcdef"),
334+
?ASSERT_MATCH(lists:append([1, 2], [3, 4]), [1, 2, 3, 4]),
335+
?ASSERT_ERROR(lists:append(1, 3), badarg),
336+
ok.
337+
324338
id(X) -> X.

0 commit comments

Comments
 (0)