Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import Data.List (sort, group, sortBy, groupBy)
- import Data.List.Split
- import Control.Arrow ((&&&))
- file = ["You know i'm getting real sick of frustation frustation frustation frustation frustation frustation all this SHIT!!! Haskell irritates me"]
- simpleMapReduce :: (a -> b) -> ([b] -> c) -> [a] -> c
- simpleMapReduce mapFunction reduceFunction input = reduceFunction (map mapFunction input)
- stringSplitter str = splitOn " " str
- mapFunc :: String -> [(String,Int)]
- mapFunc str= getCountOFWords (splitOn " " str)
- getCountOFWords :: [String] -> [(String,Int)]
- getCountOFWords = map (head &&& length) . group . sort
- sortTuple (a1,b1) (a2,b2) = compare a1 a2
- groupTuple (a1,b1) (a2,b2) = a1 == a2
- combining :: [(String,Int)] -> (String,Int)
- combining ((word,value):xs) = (word, (value + summation xs))
- summation :: [(String,Int)] -> Int
- summation [] = 0
- summation ((word,value):xs) = value + summation xs
- reducer :: [[(String,Int)]] -> [(String,Int)]
- reducer list = map combining (groupBy groupTuple (sortBy sortTuple (concat list)))
- run = simpleMapReduce mapFunc reducer file
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement