[Haskell-beginners] Too much mapM, fmap and concat

Frerich Raabe raabe at froglogic.com
Thu Aug 14 07:39:01 UTC 2014


On 2014-08-13 18:21, martin wrote:
> Maybe these things just lie in the nature of the problem ("process a 
> number of files"). Otherwise any style suggestions
> would be much appreciated.

I think a lot of this is because you're hopping into/outof the IO monad all 
the time. The first thing which caught my
eye is

     result <- mapM (return . processFile) flines

...which is just

     let result = map processFile flines

Seeing that you then just concatenate those entries and print them, you can 
drop this definition altogether and merge it
with the next line so that it says

     mapM_ putStrLn (concatMap processFile flines)

The next thing I noticed isthat your nameAndContent function un-wraps the 
result of "fmap lines $ readFile fn" only
to then wrap it again:

     nameAndContent fn = do
         content <- fmap lines $ readFile fn
         return (fn, content)

Much like you fmap'ed "lines" on the IO [String] returned by 'readFile fn', 
you can also fmap "((,) fn)" on the result. If
you also use the "<$>" alias for fmap, this becomes

     nameAndContent fn = ((,) fn) . lines <$> readFile fn

-- 
Frerich Raabe - raabe at froglogic.com
www.froglogic.com - Multi-Platform GUI Testing


More information about the Beginners mailing list