1
1
mirror of https://github.com/thma/LtuPatternFactory.git synced 2024-12-12 10:42:08 +03:00

start with map reduce

This commit is contained in:
thma 2019-01-05 11:56:36 +01:00
parent 13fa2e13c6
commit d19f5159f0

38
src/MapReduce.hs Normal file
View File

@ -0,0 +1,38 @@
module MapReduce where
import Data.Char (toLower)
import Data.List (sort, group)
import Control.Arrow ((&&&))
import Data.Map as Map hiding (map, filter)
import Data.Monoid
stringToWordCountMap :: String -> Map.Map String Int
stringToWordCountMap = Map.fromList . map (head &&& length) . group . sort . words . map toLower
combineWordCountMaps :: Map.Map String Int -> Map.Map String Int -> Map.Map String Int
combineWordCountMaps = Map.unionWith (+)
reduceWordCountMaps :: [Map.Map String Int] -> Map.Map String Int
reduceWordCountMaps [x] = x
reduceWordCountMaps (x:xs) = combineWordCountMaps x (reduceWordCountMaps xs)
--countWords :: String -> Map.Map String Int
--countWords =
simpleMapReduce
:: (a -> b) -- map function
-> ([b] -> c) -- reduce function
-> [a] -- list to map over
-> c -- result
simpleMapReduce mapFunc reduceFunc = reduceFunc . Prelude.map mapFunc
filterBy :: String -> Char -> Bool
filterBy notAllowedChars char =
char `notElem` notAllowedChars
mapReduceDemo = do
contents <- readFile "LICENSE"
let linesInFile = lines $ filter (filterBy ".,;:!?()[]{}\"'") contents
let result = simpleMapReduce stringToWordCountMap reduceWordCountMaps linesInFile
putStrLn $ "The file has " ++ show (length (lines contents)) ++ " lines!"
putStrLn $ "result = " ++ show result ++ "."