packages feed

safe-lazy-io-0.1: examples/count-words.hs

import qualified System.IO.Lazy.Input as LI
import System.Environment (getArgs)

main = print =<< LI.run . fmap (length . words) . LI.concat . map LI.readFile =<< getArgs

-- some experimentations

-- main = print =<< fmap sum . mapM (fmap (length . words) . readFile) =<< getArgs
-- main = print =<< LI.run . fmap sum . mapM (fmap (length . words) . LI.readFile) =<< getArgs
-- main = print =<< LI.run . fmap (length . words) . concatLI . map LI.readFile =<< getArgs
-- main = print =<< LI.run . fmap sum . LI.sequence . map (fmap (length . words) . LI.readFile) =<< getArgs
-- main = print =<< LI.run . fmap (length . words . D.toList . runW) . concatLI . map (fmap (W . D.fromList)) . map LI.readFile =<< getArgs

{-
main =
  do args <- getArgs
     let onEachFile filePath = do contents <- LI.readFile filePath
                                  return $ length $ words contents
     i <- LI.run $ do
            counts <- mapM onEachFile args
            return $ sum counts
     print i
-}