Skip to content

Commit 67157f5

Browse files
committed
Merge pull request #1298 from pguyot/w39/add-support-for-lists-nthtail
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
2 parents 6c2ed33 + 346bef7 commit 67157f5

File tree

3 files changed

+28
-0
lines changed

3 files changed

+28
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ also non string parameters (e.g. `Enum.join([1, 2], ",")`
3030
- Support for `binary:split/3` and `string:find/2,3`
3131
- Support for large tuples (more than 255 elements) in external terms.
3232
- Support for `io:put_chars/2`
33+
- Support for `lists:nthtail/2`
3334

3435
### Changed
3536

libs/estdlib/src/lists.erl

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
-export([
3333
map/2,
3434
nth/2,
35+
nthtail/2,
3536
last/1,
3637
member/2,
3738
delete/2,
@@ -92,6 +93,22 @@ nth(1, [H | _T]) ->
9293
nth(Index, [_H | T]) when Index > 1 ->
9394
nth(Index - 1, T).
9495

96+
%%-----------------------------------------------------------------------------
97+
%% @param N the index to start the sublist from
98+
%% @param L the list from which to extract a tail
99+
%% @returns a sublist of list starting from position N.
100+
%% @doc Get the sublist of list L starting after the element N.
101+
%%
102+
%% The behavior of this function is undefined if N is outside of the
103+
%% {0..length(L)}.
104+
%% @end
105+
%%-----------------------------------------------------------------------------
106+
-spec nthtail(N :: non_neg_integer(), L :: list()) -> list().
107+
nthtail(0, L) when is_list(L) ->
108+
L;
109+
nthtail(N, [_H | T]) when is_integer(N) andalso N > 0 ->
110+
nthtail(N - 1, T).
111+
95112
%%-----------------------------------------------------------------------------
96113
%% @param L the proper list from which to get the last item
97114
%% @returns the last item of the list.

tests/libs/estdlib/test_lists.erl

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626

2727
test() ->
2828
ok = test_nth(),
29+
ok = test_nthtail(),
2930
ok = test_member(),
3031
ok = test_delete(),
3132
ok = test_keyfind(),
@@ -57,6 +58,15 @@ test_nth() ->
5758
?ASSERT_ERROR(lists:nth(-1, [a, b, c]), function_clause),
5859
ok.
5960

61+
test_nthtail() ->
62+
?ASSERT_MATCH(lists:nthtail(0, [a, b, c]), [a, b, c]),
63+
?ASSERT_MATCH(lists:nthtail(1, [a, b, c]), [b, c]),
64+
?ASSERT_MATCH(lists:nthtail(2, [a, b, c]), [c]),
65+
?ASSERT_MATCH(lists:nthtail(3, [a, b, c]), []),
66+
?ASSERT_ERROR(lists:nthtail(-1, [a, b, c]), function_clause),
67+
?ASSERT_ERROR(lists:nthtail(4, [a, b, c]), function_clause),
68+
ok.
69+
6070
test_member() ->
6171
?ASSERT_TRUE(lists:member(a, [a, b, c])),
6272
?ASSERT_TRUE(lists:member(b, [a, b, c])),

0 commit comments

Comments
 (0)