Skip to content

Commit aa2a6f4

Browse files
committed
Add lists:dropwhile/2, fix lists:duplicate/2
Signed-off-by: Jakub Gonet <jakub.gonet@swmansion.com>
1 parent bafd18b commit aa2a6f4

File tree

3 files changed

+49
-1
lines changed

3 files changed

+49
-1
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
2222
- Added support for `ets:update_counter/3` and `ets:update_counter/4`.
2323
- Added `erlang:+/1`
2424
- Added `lists:append/1` and `lists:append/2`
25+
- Support for zero count in `lists:duplicate/2`.
26+
- Added `lists:dropwhile/2`.
2527

2628
### Fixed
2729
- ESP32: improved sntp sync speed from a cold boot.

libs/estdlib/src/lists.erl

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@
6060
sort/1, sort/2,
6161
split/2,
6262
usort/1, usort/2,
63+
dropwhile/2,
6364
duplicate/2,
6465
sublist/2,
6566
append/1,
@@ -730,6 +731,32 @@ unique([X, Y | Tail], Fun) ->
730731
false ->
731732
[X | unique([Y | Tail], Fun)]
732733
end.
734+
735+
%%-----------------------------------------------------------------------------
736+
%% @param Pred the predicate to check against elements in List1
737+
%% @param List1
738+
%% @returns List List1 tail at the point predicate returned false for a element
739+
%% @doc Drops elements Elem from List1 while Pred(Elem) returns true and returns
740+
%% the remaining list. The Pred function must return a boolean.
741+
%% @end
742+
%%-----------------------------------------------------------------------------
743+
% Attribution: https://github.com/erlang/otp/blob/05737d130706c7189a8e6750d9c2252d2cc7987e/lib/stdlib/src/lists.erl#L2403
744+
-spec dropwhile(Pred, List1) -> List2 when
745+
Pred :: fun((Elem :: T) -> boolean()),
746+
List1 :: [T],
747+
List2 :: [T],
748+
T :: term().
749+
750+
dropwhile(Pred, List) when is_function(Pred, 1) ->
751+
dropwhile_1(Pred, List).
752+
753+
dropwhile_1(Pred, [Hd | Tail] = Rest) ->
754+
case Pred(Hd) of
755+
true -> dropwhile_1(Pred, Tail);
756+
false -> Rest
757+
end;
758+
dropwhile_1(_Pred, []) ->
759+
[].
733760

734761
%%-----------------------------------------------------------------------------
735762
%% @param Elem the element to duplicate
@@ -739,7 +766,7 @@ unique([X, Y | Tail], Fun) ->
739766
%% @end
740767
%%-----------------------------------------------------------------------------
741768
-spec duplicate(integer(), Elem) -> [Elem].
742-
duplicate(Count, Elem) when is_integer(Count) andalso Count > 0 ->
769+
duplicate(Count, Elem) when is_integer(Count) andalso Count >= 0 ->
743770
duplicate(Count, Elem, []).
744771

745772
duplicate(0, _Elem, Acc) -> Acc;

tests/libs/estdlib/test_lists.erl

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@ test() ->
4747
ok = test_sort(),
4848
ok = test_split(),
4949
ok = test_usort(),
50+
ok = test_dropwhile(),
51+
ok = test_duplicate(),
5052
ok = test_filtermap(),
5153
ok = test_last(),
5254
ok = test_mapfoldl(),
@@ -294,6 +296,23 @@ test_usort() ->
294296
?ASSERT_ERROR(lists:usort(1, [1]), function_clause),
295297

296298
ok.
299+
300+
test_dropwhile() ->
301+
?ASSERT_MATCH(lists:dropwhile(fun(_X) -> true end, []), []),
302+
?ASSERT_MATCH(lists:dropwhile(fun(_X) -> false end, []), []),
303+
?ASSERT_MATCH(lists:dropwhile(fun(_X) -> false end, [1]), [1]),
304+
?ASSERT_MATCH(lists:dropwhile(fun(X) -> X == 1 end, [1, 1, 1, 2, 3]), [2, 3]),
305+
?ASSERT_ERROR(lists:dropwhile([], []), function_clause),
306+
?ASSERT_ERROR(lists:dropwhile(fun(X) -> X == 1 end, [1 | 1]), function_clause),
307+
ok.
308+
309+
test_duplicate() ->
310+
?ASSERT_MATCH(lists:duplicate(0, x), []),
311+
?ASSERT_MATCH(lists:duplicate(1, x), [x]),
312+
?ASSERT_MATCH(lists:duplicate(3, []), [[], [], []]),
313+
?ASSERT_ERROR(lists:duplicate(-1, x), function_clause),
314+
?ASSERT_ERROR(lists:duplicate(x, x), function_clause),
315+
ok.
297316

298317
test_filtermap() ->
299318
?ASSERT_MATCH(

0 commit comments

Comments
 (0)