Skip to content

Instantly share code, notes, and snippets.

@hiratara
Last active November 10, 2018 02:20
Show Gist options
  • Save hiratara/8079fb67a1152dace92af8c9f5fd5f46 to your computer and use it in GitHub Desktop.
Save hiratara/8079fb67a1152dace92af8c9f5fd5f46 to your computer and use it in GitHub Desktop.
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