Skip to content

Commit f016904

Browse files
committed
Merge pull request #1629 from pguyot/w15/add-lists-flatmap-2
Add `lists:flatmap/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 75d6d2d + 7b4f5ed commit f016904

File tree

3 files changed

+63
-0
lines changed

3 files changed

+63
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
2828
- Added `erlang:get/0` and `erlang:erase/0`.
2929
- Added `erlang:unique_integer/0` and `erlang:unique_integer/1`
3030
- Added support for 'ets:delete/1'.
31+
- Added `lists:flatmap/2`
3132

3233
### Fixed
3334
- ESP32: improved sntp sync speed from a cold boot.

libs/estdlib/src/lists.erl

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@
5252
all/2,
5353
any/2,
5454
flatten/1,
55+
flatmap/2,
5556
search/2,
5657
filter/2,
5758
filtermap/2,
@@ -489,6 +490,23 @@ flatten([H | T], Accum) ->
489490

490491
%% post: return is flattened
491492

493+
%%-----------------------------------------------------------------------------
494+
%% @param F the function to apply to elements of L
495+
%% @param L the list to map
496+
%% @returns List of mapped elements, appended
497+
%% @doc Map elements of list L by applying F, and append the results. F should
498+
%% return a list.
499+
%% @end
500+
%%-----------------------------------------------------------------------------
501+
-spec flatmap(F :: fun((Elem :: term()) -> [term()]), List :: [term()]) -> [term()].
502+
flatmap(F, L) when is_list(L) ->
503+
flatmap_1(F, L).
504+
505+
flatmap_1(_F, []) ->
506+
[];
507+
flatmap_1(F, [H | T]) ->
508+
F(H) ++ flatmap_1(F, T).
509+
492510
%%-----------------------------------------------------------------------------
493511
%% @param Pred the predicate to apply to elements in List
494512
%% @param List search

tests/libs/estdlib/test_lists.erl

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ test() ->
4141
ok = test_any(),
4242
ok = test_list_match(),
4343
ok = test_flatten(),
44+
ok = test_flatmap(),
4445
ok = test_filter(),
4546
ok = test_join(),
4647
ok = test_seq(),
@@ -207,6 +208,49 @@ test_flatten() ->
207208
),
208209
ok.
209210

211+
test_flatmap() ->
212+
?ASSERT_MATCH(lists:flatmap(fun(X) -> X + 1 end, []), []),
213+
?ASSERT_MATCH(lists:flatmap(fun(X) -> [X + 1] end, [1]), [2]),
214+
?ASSERT_MATCH(lists:flatmap(fun(X) -> [X * X] end, [1, 2, 3, 4, 5]), [1, 4, 9, 16, 25]),
215+
?ASSERT_MATCH(
216+
lists:flatmap(
217+
fun(X) ->
218+
case X rem 2 of
219+
0 -> [X + 1];
220+
1 -> [X + 2]
221+
end
222+
end,
223+
[1, 2]
224+
),
225+
[3, 3]
226+
),
227+
?ASSERT_MATCH(
228+
lists:flatmap(
229+
fun(X) ->
230+
case X rem 2 of
231+
0 -> [[X + 1]];
232+
1 -> [X + 2]
233+
end
234+
end,
235+
[1, 2]
236+
),
237+
[3, [3]]
238+
),
239+
?ASSERT_MATCH(
240+
lists:flatmap(
241+
fun(X) ->
242+
case X rem 2 of
243+
0 -> [X + 1];
244+
1 -> [X + 1, X + 2]
245+
end
246+
end,
247+
[1, 4]
248+
),
249+
[2, 3, 5]
250+
),
251+
?ASSERT_ERROR(lists:flatmap(fun(X) -> X + 1 end, [1]), badarg),
252+
ok.
253+
210254
test_filter() ->
211255
?ASSERT_MATCH(lists:filter(fun(I) -> I rem 2 =:= 0 end, []), []),
212256
?ASSERT_MATCH(lists:filter(fun(I) -> I rem 2 =:= 0 end, [1, 2, 3, 4, 5]), [2, 4]),

0 commit comments

Comments
 (0)