Skip to content

Commit afdecae

Browse files
committed
Merge pull request #1565 from jakub-gonet/jgonet/dropwhile
Add lists:dropwhile, fix lists:duplicate/2 See also #1509 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 f583afc + f6b4995 commit afdecae

File tree

5 files changed

+90
-43
lines changed

5 files changed

+90
-43
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
2323
- Added `erlang:+/1`
2424
- Added `lists:append/1` and `lists:append/2`
2525
- Added `erlang:spawn_monitor/1`, `erlang:spawn_monitor/3`
26+
- Added `lists:dropwhile/2`.
2627

2728
### Fixed
2829
- ESP32: improved sntp sync speed from a cold boot.
2930
- Utilize reserved `phy_init` partition on ESP32 to store wifi calibration for faster connections.
31+
- Support for zero count in `lists:duplicate/2`.
3032

3133
## [0.6.6] - Unreleased
3234

libs/eavmlib/src/esp.erl

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -486,7 +486,7 @@ nvs_reformat() ->
486486
%% @end
487487
%%-----------------------------------------------------------------------------
488488
-spec partition_erase_range(Partition_id :: binary(), Offset :: non_neg_integer()) -> ok | error.
489-
partition_erase_range(Partition_id, Offset) ->
489+
partition_erase_range(_Partition_id, _Offset) ->
490490
erlang:nif_error(undefined).
491491

492492
%%-----------------------------------------------------------------------------
@@ -505,7 +505,7 @@ partition_erase_range(Partition_id, Offset) ->
505505
-spec partition_erase_range(
506506
Partition_id :: binary(), Offset :: non_neg_integer(), Size :: pos_integer()
507507
) -> ok | error.
508-
partition_erase_range(Partition_id, Offset, Size) ->
508+
partition_erase_range(_Partition_id, _Offset, _Size) ->
509509
erlang:nif_error(undefined).
510510

511511
%%-----------------------------------------------------------------------------
@@ -531,7 +531,7 @@ partition_list() ->
531531
-spec partition_read(
532532
Partition_id :: binary(), Offset :: non_neg_integer(), Read_size :: non_neg_integer()
533533
) -> {ok, binary()} | error.
534-
partition_read(Partition_id, Offset, Read_size) ->
534+
partition_read(_Partition_id, _Offset, _Read_size) ->
535535
erlang:nif_error(undefined).
536536

537537
%%-----------------------------------------------------------------------------
@@ -550,7 +550,7 @@ partition_read(Partition_id, Offset, Read_size) ->
550550
%%-----------------------------------------------------------------------------
551551
-spec partition_write(Partition_id :: binary(), Offset :: non_neg_integer(), Data :: binary()) ->
552552
ok | error.
553-
partition_write(Partition_id, Offset, Data) ->
553+
partition_write(_Partition_id, _Offset, _Data) ->
554554
erlang:nif_error(undefined).
555555

556556
%%-----------------------------------------------------------------------------

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,
@@ -731,6 +732,32 @@ unique([X, Y | Tail], Fun) ->
731732
[X | unique([Y | Tail], Fun)]
732733
end.
733734

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+
[].
760+
734761
%%-----------------------------------------------------------------------------
735762
%% @param Elem the element to duplicate
736763
%% @param Count the number of times to duplicate the element
@@ -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/erlang_tests/unary_plus.erl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,11 @@ start() ->
2929
0.
3030

3131
unary_plus_int(A) ->
32-
+A,
32+
A = +A,
3333
ok.
3434

3535
unary_plus_float(A) ->
36-
+A,
36+
A = +A,
3737
ok.
3838

3939
unary_plus_str(A) ->

tests/libs/estdlib/test_lists.erl

Lines changed: 54 additions & 36 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(),
@@ -99,54 +101,45 @@ test_keydelete() ->
99101
?ASSERT_MATCH(lists:keydelete(a, 1, []), []),
100102
?ASSERT_MATCH(lists:keydelete(a, 1, [{a, x}, b, []]), [b, []]),
101103
?ASSERT_MATCH(lists:keydelete(a, 1, [b, {a, x}, []]), [b, []]),
102-
?ASSERT_MATCH(lists:keydelete(a, 1, [b, {a, x}, {a, x}, {a, x}, []]), [b, {a, x}, {a, x}, []]),
104+
?ASSERT_MATCH(
105+
lists:keydelete(a, 1, [b, {a, x}, {a, x}, {a, x}, []]),
106+
[b, {a, x}, {a, x}, []]
107+
),
103108
ok.
104109

105110
test_keyreplace() ->
106111
?ASSERT_MATCH(lists:keyreplace(a, 1, [], {foo, bar}), []),
107112
?ASSERT_MATCH(lists:keyreplace(a, 1, [{a, x}, b, []], {1, 2}), [{1, 2}, b, []]),
108113
?ASSERT_MATCH(lists:keyreplace(x, 2, [b, {a, x}, []], {1, 2}), [b, {1, 2}, []]),
109-
?ASSERT_MATCH(lists:keyreplace(a, 1, [b, {a, x}, {a, x}, {a, x}, []], {1, 2}), [
110-
b,
111-
{1, 2},
112-
{a, x},
113-
{a, x},
114-
[]
115-
]),
116-
?ASSERT_MATCH(lists:keyreplace(a, 3, [b, {a, x}, {a, x}, {a, x}, []], {1, 2}), [
117-
b,
118-
{a, x},
119-
{a, x},
120-
{a, x},
121-
[]
122-
]),
114+
?ASSERT_MATCH(
115+
lists:keyreplace(a, 1, [b, {a, x}, {a, x}, {a, x}, []], {1, 2}),
116+
[b, {1, 2}, {a, x}, {a, x}, []]
117+
),
118+
?ASSERT_MATCH(
119+
lists:keyreplace(a, 3, [b, {a, x}, {a, x}, {a, x}, []], {1, 2}),
120+
[b, {a, x}, {a, x}, {a, x}, []]
121+
),
123122
ok.
124123

125124
test_keystore() ->
126125
?ASSERT_MATCH(lists:keystore(a, 1, [], {foo, bar}), [{foo, bar}]),
127126
?ASSERT_MATCH(lists:keystore(a, 1, [{a, x}, b, []], {1, 2}), [{1, 2}, b, []]),
128127
?ASSERT_MATCH(lists:keystore(x, 2, [b, {a, x}, []], {1, 2}), [b, {1, 2}, []]),
129-
?ASSERT_MATCH(lists:keystore(a, 1, [b, {a, x}, {a, x}, {a, x}, []], {1, 2}), [
130-
b,
131-
{1, 2},
132-
{a, x},
133-
{a, x},
134-
[]
135-
]),
136-
?ASSERT_MATCH(lists:keystore(a, 3, [b, {a, x}, {a, x}, {a, x}, []], {1, 2}), [
137-
b,
138-
{a, x},
139-
{a, x},
140-
{a, x},
141-
[],
142-
{1, 2}
143-
]),
128+
?ASSERT_MATCH(
129+
lists:keystore(a, 1, [b, {a, x}, {a, x}, {a, x}, []], {1, 2}),
130+
[b, {1, 2}, {a, x}, {a, x}, []]
131+
),
132+
?ASSERT_MATCH(
133+
lists:keystore(a, 3, [b, {a, x}, {a, x}, {a, x}, []], {1, 2}),
134+
[b, {a, x}, {a, x}, {a, x}, [], {1, 2}]
135+
),
144136
ok.
145137

146138
test_keytake() ->
147139
List1 = [{name, "Joe"}, {name, "Robert"}, {name, "Mike"}],
148140
?ASSERT_MATCH(
149-
lists:keytake("Joe", 2, List1), {value, {name, "Joe"}, [{name, "Robert"}, {name, "Mike"}]}
141+
lists:keytake("Joe", 2, List1),
142+
{value, {name, "Joe"}, [{name, "Robert"}, {name, "Mike"}]}
150143
),
151144
?ASSERT_MATCH(
152145
lists:keytake("Robert", 2, List1),
@@ -208,7 +201,10 @@ test_flatten() ->
208201
?ASSERT_MATCH(lists:flatten([[a], [b]]), [a, b]),
209202
?ASSERT_MATCH(lists:flatten([[a], [b, [c]]]), [a, b, c]),
210203
?ASSERT_MATCH(lists:flatten([[a], [b, {c, [d, [e, [f]]]}]]), [a, b, {c, [d, [e, [f]]]}]),
211-
?ASSERT_MATCH(lists:flatten([[a, b, c], [d, e, f], [g, h, i]]), [a, b, c, d, e, f, g, h, i]),
204+
?ASSERT_MATCH(
205+
lists:flatten([[a, b, c], [d, e, f], [g, h, i]]),
206+
[a, b, c, d, e, f, g, h, i]
207+
),
212208
ok.
213209

214210
test_filter() ->
@@ -243,7 +239,6 @@ test_seq() ->
243239
?ASSERT_ERROR(lists:seq(-1, 1, -1)),
244240
?ASSERT_ERROR(lists:seq(1, -1, 1)),
245241
?ASSERT_ERROR(lists:seq(1, 2, 0)),
246-
247242
ok.
248243

249244
test_sort() ->
@@ -286,13 +281,32 @@ test_usort() ->
286281
?ASSERT_MATCH(lists:usort([1, 3, 5, 2, 1, 4]), [1, 2, 3, 4, 5]),
287282
?ASSERT_MATCH(lists:usort([1, 3, 5, 2, 5, 4]), [1, 2, 3, 4, 5]),
288283

289-
?ASSERT_MATCH(lists:usort(fun(A, B) -> A > B end, [1, 2, 3, 4, 3, 5]), [5, 4, 3, 3, 2, 1]),
284+
?ASSERT_MATCH(
285+
lists:usort(fun(A, B) -> A > B end, [1, 2, 3, 4, 3, 5]),
286+
[5, 4, 3, 3, 2, 1]
287+
),
290288
?ASSERT_MATCH(lists:usort(fun(A, B) -> A >= B end, [1, 2, 3, 4, 3, 5]), [5, 4, 3, 2, 1]),
291289

292290
?ASSERT_ERROR(lists:usort(1), function_clause),
293291
?ASSERT_ERROR(lists:usort(fun(A, B) -> A > B end, 1), function_clause),
294292
?ASSERT_ERROR(lists:usort(1, [1]), function_clause),
293+
ok.
294+
295+
test_dropwhile() ->
296+
?ASSERT_MATCH(lists:dropwhile(fun(_X) -> true end, []), []),
297+
?ASSERT_MATCH(lists:dropwhile(fun(_X) -> false end, []), []),
298+
?ASSERT_MATCH(lists:dropwhile(fun(_X) -> false end, [1]), [1]),
299+
?ASSERT_MATCH(lists:dropwhile(fun(X) -> X == 1 end, [1, 1, 1, 2, 3]), [2, 3]),
300+
?ASSERT_ERROR(lists:dropwhile([], []), function_clause),
301+
?ASSERT_ERROR(lists:dropwhile(fun(X) -> X == 1 end, [1 | 1]), function_clause),
302+
ok.
295303

304+
test_duplicate() ->
305+
?ASSERT_MATCH(lists:duplicate(0, x), []),
306+
?ASSERT_MATCH(lists:duplicate(1, x), [x]),
307+
?ASSERT_MATCH(lists:duplicate(3, []), [[], [], []]),
308+
?ASSERT_ERROR(lists:duplicate(-1, x), function_clause),
309+
?ASSERT_ERROR(lists:duplicate(x, x), function_clause),
296310
ok.
297311

298312
test_filtermap() ->
@@ -319,7 +333,10 @@ test_last() ->
319333

320334
test_mapfoldl() ->
321335
?ASSERT_MATCH({[], 1}, lists:mapfoldl(fun(X, A) -> {X * A, A + 1} end, 1, [])),
322-
?ASSERT_MATCH({[1, 4, 9], 4}, lists:mapfoldl(fun(X, A) -> {X * A, A + 1} end, 1, [1, 2, 3])),
336+
?ASSERT_MATCH(
337+
{[1, 4, 9], 4},
338+
lists:mapfoldl(fun(X, A) -> {X * A, A + 1} end, 1, [1, 2, 3])
339+
),
323340
?ASSERT_ERROR(lists:mapfoldl(fun(X, A) -> {X * A, A + 1} end, 1, foo), function_clause),
324341
ok.
325342

@@ -335,4 +352,5 @@ test_append() ->
335352
?ASSERT_ERROR(lists:append(1, 3), badarg),
336353
ok.
337354

338-
id(X) -> X.
355+
id(X) ->
356+
X.

0 commit comments

Comments
 (0)