Skip to content

Instantly share code, notes, and snippets.

@mmitou
Created December 4, 2012 11:27
Show Gist options
  • Select an option

  • Save mmitou/4202869 to your computer and use it in GitHub Desktop.

Select an option

Save mmitou/4202869 to your computer and use it in GitHub Desktop.
Hadoopテスト reducer
import System.IO
import Control.Applicative
import Control.Monad
import Text.Parsec
import Text.Parsec.String
import Data.Map
import Data.Char
wordCount' :: Parser (String, Int)
wordCount' = do
word <- many1 letter
skipMany1 space
nstr <- digit
return (word, c2i nstr)
where
c2i c = ord c - ord '0'
wordCount :: Parser (String, Int)
wordCount = (\x y -> (x, (read y))) <$> ((many1 letter) <* spaces) <*> (many1 digit)
updateMap :: (Ord k, Num v) => k -> v -> Map k v -> Map k v
updateMap k v m | Data.Map.member k m = adjust (+ v) k m
| otherwise = insert k v m
loop :: Map String Int -> IO (Map String Int)
loop m = do
isEnd <- isEOF
if isEnd then
return m
else do
str <- getLine
case parse wordCount "" str of
Right (k, v) -> loop $ updateMap k v m
_ -> loop m
main = do
m <- loop $ fromList []
forM_ (toList m) $ \(s,n) -> putStrLn (s ++ "\t" ++ (show n))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment