Skip to content

Commit

Permalink
Merge pull request #8749 from sabiwara/optimize-filtermap
Browse files Browse the repository at this point in the history
Optimize lists:flatmap/2 for filtering
  • Loading branch information
bjorng authored Aug 28, 2024
2 parents 36b217f + 29bb2b4 commit 5fa0be7
Showing 1 changed file with 6 additions and 1 deletion.
7 changes: 6 additions & 1 deletion lib/stdlib/src/lists.erl
Original file line number Diff line number Diff line change
Expand Up @@ -2113,7 +2113,12 @@ flatmap(F, List) when is_function(F, 1) ->
flatmap_1(F, List).

flatmap_1(F, [Hd | Tail]) ->
F(Hd) ++ flatmap_1(F, Tail);
case F(Hd) of
%% The two first clauses are an optimization.
[] -> flatmap_1(F, Tail);
[Elem] -> [Elem | flatmap_1(F, Tail)];
List -> List ++ flatmap_1(F, Tail)
end;
flatmap_1(_F, []) ->
[].

Expand Down

0 comments on commit 5fa0be7

Please sign in to comment.