Skip to content

Commit

Permalink
Merge pull request #1298 from pguyot/w39/add-support-for-lists-nthtail
Browse files Browse the repository at this point in the history
Add support for `lists:nthtail/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 Sep 29, 2024
2 parents 6c2ed33 + 346bef7 commit 67157f5
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ also non string parameters (e.g. `Enum.join([1, 2], ",")`
- Support for `binary:split/3` and `string:find/2,3`
- Support for large tuples (more than 255 elements) in external terms.
- Support for `io:put_chars/2`
- Support for `lists:nthtail/2`

### Changed

Expand Down
17 changes: 17 additions & 0 deletions libs/estdlib/src/lists.erl
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
-export([
map/2,
nth/2,
nthtail/2,
last/1,
member/2,
delete/2,
Expand Down Expand Up @@ -92,6 +93,22 @@ nth(1, [H | _T]) ->
nth(Index, [_H | T]) when Index > 1 ->
nth(Index - 1, T).

%%-----------------------------------------------------------------------------
%% @param N the index to start the sublist from
%% @param L the list from which to extract a tail
%% @returns a sublist of list starting from position N.
%% @doc Get the sublist of list L starting after the element N.
%%
%% The behavior of this function is undefined if N is outside of the
%% {0..length(L)}.
%% @end
%%-----------------------------------------------------------------------------
-spec nthtail(N :: non_neg_integer(), L :: list()) -> list().
nthtail(0, L) when is_list(L) ->
L;
nthtail(N, [_H | T]) when is_integer(N) andalso N > 0 ->
nthtail(N - 1, T).

%%-----------------------------------------------------------------------------
%% @param L the proper list from which to get the last item
%% @returns the last item of the list.
Expand Down
10 changes: 10 additions & 0 deletions tests/libs/estdlib/test_lists.erl
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@

test() ->
ok = test_nth(),
ok = test_nthtail(),
ok = test_member(),
ok = test_delete(),
ok = test_keyfind(),
Expand Down Expand Up @@ -57,6 +58,15 @@ test_nth() ->
?ASSERT_ERROR(lists:nth(-1, [a, b, c]), function_clause),
ok.

test_nthtail() ->
?ASSERT_MATCH(lists:nthtail(0, [a, b, c]), [a, b, c]),
?ASSERT_MATCH(lists:nthtail(1, [a, b, c]), [b, c]),
?ASSERT_MATCH(lists:nthtail(2, [a, b, c]), [c]),
?ASSERT_MATCH(lists:nthtail(3, [a, b, c]), []),
?ASSERT_ERROR(lists:nthtail(-1, [a, b, c]), function_clause),
?ASSERT_ERROR(lists:nthtail(4, [a, b, c]), function_clause),
ok.

test_member() ->
?ASSERT_TRUE(lists:member(a, [a, b, c])),
?ASSERT_TRUE(lists:member(b, [a, b, c])),
Expand Down

0 comments on commit 67157f5

Please sign in to comment.