Advertisement
Guest User

Untitled

a guest
Nov 21st, 2014
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import Data.List (sort, group, sortBy, groupBy)
  2. import Data.List.Split
  3. import Control.Arrow ((&&&))
  4.  
  5. file = ["You know i'm getting real sick of frustation frustation frustation frustation frustation frustation all this SHIT!!! Haskell irritates me"]
  6.  
  7.  
  8. simpleMapReduce :: (a -> b) -> ([b] -> c) -> [a] -> c
  9. simpleMapReduce mapFunction reduceFunction input = reduceFunction (map mapFunction input)
  10.  
  11. stringSplitter str = splitOn " " str
  12.  
  13. mapFunc :: String -> [(String,Int)]
  14. mapFunc str= getCountOFWords (splitOn " " str)
  15.  
  16. getCountOFWords :: [String] -> [(String,Int)]
  17. getCountOFWords = map (head &&& length) . group . sort
  18.  
  19. sortTuple (a1,b1) (a2,b2) = compare a1 a2
  20. groupTuple (a1,b1) (a2,b2) = a1 == a2
  21.  
  22. combining :: [(String,Int)] -> (String,Int)
  23. combining ((word,value):xs) = (word, (value + summation xs))
  24.  
  25. summation :: [(String,Int)] -> Int
  26. summation [] = 0
  27. summation ((word,value):xs) = value + summation xs
  28.  
  29. reducer :: [[(String,Int)]] -> [(String,Int)]
  30. reducer list = map combining (groupBy groupTuple (sortBy sortTuple (concat list)))
  31.  
  32. run = simpleMapReduce mapFunc reducer file
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement