Created
May 20, 2014 06:16
-
-
Save moizjv/4c7fb1b6b39005b086be to your computer and use it in GitHub Desktop.
parallel file reads in haskell
This file contains 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
readFilesFromDirectory :: IO [([Char], [([Char], Int)])] | |
readFilesFromDirectory = do | |
allFiles <- getDirectoryContents "docs" | |
let filtered = filter (endswith ".htm") allFiles | |
--listOfTuplesWithFileContent <- readingFiles filtered | |
listOfTuplesWithFileContent <- combingReadFiles $ readingFilesInParallel filtered | |
return listOfTuplesWithFileContent | |
stripTags :: String -> String | |
stripTags [] = [] | |
stripTags ('<' : xs) = stripTags $ drop 1 $ dropWhile (/= '>') xs | |
stripTags (x : xs) = x : stripTags xs | |
combingReadFiles input = sequence input | |
readingFilesInParallel names = parMap rpar (\x -> do | |
temp <- readFile $ "docs/" ++ x | |
return (x, mapper $ stripTags $ temp)) names | |
readingFiles :: [[Char]] -> IO [([Char], [(String, Int)])] | |
readingFiles [] = return [] | |
readingFiles (x:xs) = do | |
temp <- readFile $ "docs/" ++ x | |
rest <- readingFiles xs | |
return ((x, mapper $ stripTags $ temp):rest) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment