Created
April 7, 2014 13:04
-
-
Save iporsut/10019912 to your computer and use it in GitHub Desktop.
Haskell Map Reduce
This file contains hidden or 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 Data.Char | |
import Data.List | |
groupWord:: [(String,Integer)] -> [[(String,Integer)]] | |
sortLT:: (String, Integer) -> (String, Integer) -> Ordering | |
filterWord = filter (all isAlphaNum) . words | |
mapWordCounter = map (\w -> (w,1)) | |
sortLT (w1,_) (w2,_) | |
| w1 < w2 = LT | |
| w1 > w2 = GT | |
| w1 == w2 = EQ | |
sortMap = sortBy sortLT | |
groupWord = groupBy (\(w1,_) (w2,_) -> w1 == w2) | |
mapGroupWord mapGroupList= map (\((w,1):xs) -> (w, map (\(w,v) -> v) ((w,1):xs))) (groupWord$sortMap mapGroupList) | |
reduceGroupWord = map (\(w,listCounter) -> (w,sum(listCounter))) | |
mapReduce = reduceGroupWord.mapGroupWord.mapWordCounter.filterWord |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment