Skip to content

Instantly share code, notes, and snippets.

@cronin101
Last active December 28, 2015 00:09
Show Gist options
  • Select an option

  • Save cronin101/7411696 to your computer and use it in GitHub Desktop.

Select an option

Save cronin101/7411696 to your computer and use it in GitHub Desktop.
MapReduce and token-counting in Haskell.
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