Skip to main content

W0028 - Unnecessary Flattening To Find Flat Length

Error

main(NestedList) ->
length(lists:flatten(NestedList)).
%% ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 💡 warning: Unnecessary intermediate flat-list allocated.

Explanation

The warning message indicates that a list is being unnecessarily flattened.

Whilst this will correctly find the length of the flattened list, it is unnecessary to actually construct the flattened list to find that length.

To fix the issue, use the function lists:flatlength/1 to compute the flatlength without constructing an intermediate, flattened list:

main(NestedList) ->
lists:flatlength(NestedList).