ndmitchell / hlint

Haskell source code suggestions
Other
1.46k stars 195 forks source link

Is it safe to suggest `mconcat . fmap g $ xs ==> foldMap g xs` ? #1339

Open Javran opened 2 years ago

Javran commented 2 years ago

Spotted this refactoring opportunity that HLint doesn't warn me about:

> f g xs = mconcat . fmap g $ xs
> :t f
f :: Monoid c => (a -> c) -> [a] -> c
> :t foldMap
foldMap :: (Foldable t, Monoid m) => (a -> m) -> t a -> m

I know the type is different because of mconcat, but it is a safe thing to suggest?

Side note: just learnt fold while writing this up, which HLint does warn. So probably the real question is, is it aligned with HLint's goal to boost awareness for something like mconcat vs. fold?

ndmitchell commented 2 years ago

HLint could definitely suggest it - generalising sometimes makes programs break, so HLint tends not to do it that much by default, although I think some rules do it. One option would be to put this hint in the generalise for conciseness group, which definitely fits with what it does.