From 8c4f797785a8510e5662a27bfcb7359d32491de7 Mon Sep 17 00:00:00 2001 From: Hans Svensson Date: Wed, 22 Jan 2014 22:08:51 +0100 Subject: stdlib/lists: Add function droplast/1 This functions drops the last element of a non-empty list. lists:last/1 and lists:droplast/1 are the dual of hd/1 and tl/1 but for the end of a list. --- lib/stdlib/src/lists.erl | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) (limited to 'lib') diff --git a/lib/stdlib/src/lists.erl b/lib/stdlib/src/lists.erl index d6a9f4645d..6303465d3d 100644 --- a/lib/stdlib/src/lists.erl +++ b/lib/stdlib/src/lists.erl @@ -22,7 +22,7 @@ -compile({no_auto_import,[min/2]}). -export([append/2, append/1, subtract/2, reverse/1, - nth/2, nthtail/2, prefix/2, suffix/2, last/1, + nth/2, nthtail/2, prefix/2, suffix/2, droplast/1, last/1, seq/2, seq/3, sum/1, duplicate/2, min/1, max/1, sublist/2, sublist/3, delete/2, unzip/1, unzip3/1, zip/2, zip3/3, zipwith/3, zipwith3/4, @@ -203,6 +203,19 @@ suffix(Suffix, List) -> Delta = length(List) - length(Suffix), Delta >= 0 andalso nthtail(Delta, List) =:= Suffix. +%% droplast(List) returns the list dropping its last element + +-spec droplast(List) -> InitList when + List :: [T, ...], + InitList :: [T], + T :: term(). + +%% This is the simple recursive implementation +%% reverse(tl(reverse(L))) is faster on average, +%% but creates more garbage. +droplast([_T]) -> []; +droplast([H|T]) -> [H|droplast(T)]. + %% last(List) returns the last element in a list. -spec last(List) -> Last when -- cgit v1.2.3 From 254e58d2f4799e40be1188d95d59cdc7a616f5d9 Mon Sep 17 00:00:00 2001 From: Hans Svensson Date: Wed, 22 Jan 2014 22:10:14 +0100 Subject: Added tests for lists:droplast/1 to stdlib/lists_SUITE --- lib/stdlib/test/lists_SUITE.erl | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) (limited to 'lib') diff --git a/lib/stdlib/test/lists_SUITE.erl b/lib/stdlib/test/lists_SUITE.erl index 92253ef5b9..f4589a8e24 100644 --- a/lib/stdlib/test/lists_SUITE.erl +++ b/lib/stdlib/test/lists_SUITE.erl @@ -61,7 +61,7 @@ zip_unzip/1, zip_unzip3/1, zipwith/1, zipwith3/1, filter_partition/1, otp_5939/1, otp_6023/1, otp_6606/1, otp_7230/1, - suffix/1, subtract/1]). + suffix/1, subtract/1, droplast/1]). %% Sort randomized lists until stopped. %% @@ -2641,4 +2641,12 @@ sub_non_matching(A, B) -> sub(A, B) -> Res = A -- B, Res = lists:subtract(A, B). - + +%% Test lists:droplast/1 +droplast(Config) when is_list(Config) -> + ?line [] = lists:droplast([x]), + ?line [x] = lists:droplast([x, y]), + ?line {'EXIT', {function_clause, _}} = (catch lists:droplast([])), + ?line {'EXIT', {function_clause, _}} = (catch lists:droplast(x)), + + ok. -- cgit v1.2.3 From 875b58c8119858676af0139b0f6b69537cae5706 Mon Sep 17 00:00:00 2001 From: Hans Svensson Date: Wed, 22 Jan 2014 22:10:44 +0100 Subject: Added documentation of lists:droplast/1 --- lib/stdlib/doc/src/lists.xml | 8 ++++++++ 1 file changed, 8 insertions(+) (limited to 'lib') diff --git a/lib/stdlib/doc/src/lists.xml b/lib/stdlib/doc/src/lists.xml index 16f81bdba1..9c7cef066b 100644 --- a/lib/stdlib/doc/src/lists.xml +++ b/lib/stdlib/doc/src/lists.xml @@ -122,6 +122,14 @@ element.

+ + + Drop the last element of a list + +

Drops the last element of a List. The list should + be non-empty, otherwise the function will crash with a function_clause

+
+
Drop elements from a list while a predicate is true -- cgit v1.2.3