MosesA
MosesA

Reputation: 965

Haskell - Parallel list to tuples

I have a function that turns a list to a list of tuples where every key has a value of one. I want to do this by using parallelism. Should par an pseq in the parMap function or in the main_?

parMap :: [String] -> [(String, Int)]
parMap [] = []
parMap (k:xs) = do
        b <- par (k, 1)
        bs <- parMap xs
        return (b:bs)

map_ :: [String] -> [(String, Int)]
map_ [] = []
map_ (k:xs) = (k, 1) : map_ xs


main_ = do list <- getWords "test.txt"
       print $ M.toList $ reduce $ group $ map_ list

Upvotes: 0

Views: 172

Answers (1)

ErikR
ErikR

Reputation: 52029

Your definition is quite close to this way of defining parMap:

parMap :: (a -> b) -> [a] -> Eval [b]
parMap f [] = return []
parMap f (a:as) = do
   b <- rpar (f a)
   bs <- parMap f as
   return (b:bs)

To use it, call runEval on the resulting Eval monad, e.g.:

main = do list <- getWords "test.txt"
          let pairs = runEval $ parMap (\x -> (x,1)) list
          ...

Update: It looks like rpar and rseq is just another way of utilizing par and pseq as a monad. The reason I used it is because they are well documented in the book Parallel and Concurrent Programming in Haskell and the parconc package. You may also be able to find earlier revisions of the book content for free on the Web.

Upvotes: 1

Related Questions