Last active
December 28, 2015 00:09
-
-
Save cronin101/7411696 to your computer and use it in GitHub Desktop.
MapReduce and token-counting in Haskell.
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 Control.Arrow | |
| import Data.Function | |
| import Data.List | |
| import Data.Ord | |
| -- mapReduce key_func val_func reduce_func -- | |
| mapReduce :: Ord c => (b1 -> c) -> (b1 -> b) -> ([b] -> c1) -> [b1] -> [(c, c1)] | |
| mapReduce k r v = reduce' r . group' . map' k v | |
| where | |
| reduce' f = map (fst . head &&& f . map snd) | |
| group' = groupBy (on (==) fst) . sortBy (comparing fst) | |
| map' k v = map (k &&& v) | |
| tokenCount :: (Num a, Ord b) => [b] -> [(b, a)] | |
| tokenCount = mapReduce id sum (const 1) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment