Skip to main content

W0034 - Unnecessary Intermediate List In Comprehension over Map

Warning

main(List) ->
[K + V + 1 || {K,V} <- maps:to_list(Map)].
%% ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 💡 warning: Unnecessary intermediate list allocated.

Explanation

The warning message indicates an unnecessary usage of maps:to_list/1 to construct the intermediate key-value list (e.g. [{k1,v1},{k2,v2},{k3,v3},...]).

Whilst the comprehension will correctly construct a list from the given map, it is less efficient than it could be, due to the construction of the intermediate key-value list.

Using the := pattern to the left of the comprehension's <- allows operating over the map directly.

To fix this warning, build your comprehension such that it matches over the map directly:

main(List) ->
[K + V + 1 || K := V <- Map].