Last active
November 10, 2018 02:20
-
-
Save hiratara/8079fb67a1152dace92af8c9f5fd5f46 to your computer and use it in GitHub Desktop.
an answer of https://github.com/haskell-jp/makeMistakesToLearnHaskell/blob/master/assets/2.5.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Control.Monad (forM_) | |
import Data.List.Split (splitOn) | |
import qualified Data.Map as M | |
import Data.Maybe (maybe) | |
import System.Environment (getArgs) | |
import System.IO (readFile) | |
parseLine :: String -> (String, Int) | |
parseLine str = let (key:count:_) = splitOn "\t" str | |
in (key, read count) | |
main :: IO () | |
main = do | |
(path:_) <- getArgs | |
ls <- lines <$> readFile path | |
let ps = map parseLine ls | |
summary = foldr collector M.empty ps | |
forM_ (M.toList summary) $ \(k, c) -> do | |
putStrLn $ k ++ "\t" ++ show c | |
where | |
collector (k, c) = M.alter (Just . maybe c (c +)) k |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment