Skip to content

Instantly share code, notes, and snippets.

@iporsut
Created April 7, 2014 13:04
Show Gist options
  • Save iporsut/10019912 to your computer and use it in GitHub Desktop.
Save iporsut/10019912 to your computer and use it in GitHub Desktop.
Haskell Map Reduce
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