Skip to content

Instantly share code, notes, and snippets.

@moizjv
Created May 20, 2014 06:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save moizjv/4c7fb1b6b39005b086be to your computer and use it in GitHub Desktop.
Save moizjv/4c7fb1b6b39005b086be to your computer and use it in GitHub Desktop.
parallel file reads in haskell
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