Skip to main content

W0036 - unnecessary Map From List Around Comprehension

Warning

fn(List) -> maps:from_list([{K + 1, V + 2} || {K,V} <- List]).
%% ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 💡 weak: Unnecessary intermediate list allocated.

Explanation

The warning message indicates an unidiomatic and sub-optimal way to build a map from a list.

Whilst the comprehension and subsequent call to maps:from_list/1 will ultimately construct a map from the key-values pair in the given list, it is less efficient than it could be, since an intermediate list is created by the list comprehension.

Using the map comprehension syntax is clearer and more performant.

To fix this warning, replace the list comprehension and maps:from_list/1 call with a map comprehension:

main(List) ->
#{K + 1 => V + 2 || {K,V} <- List}.