diff options
| author | Ray Hogenson <rhogenson@posteo.net> | 2019-09-07 16:36:01 -0700 |
|---|---|---|
| committer | Ray Hogenson <rhogenson@posteo.net> | 2019-09-07 16:36:01 -0700 |
| commit | 440dd38f24d1df150fb5b4c7965bb6e792c929fe (patch) | |
| tree | 4d9da56e839f85949dbde1f3b3b0413f3f52b753 /Par.hs | |
| download | evolution-main.tar.zst | |
This program is in a weird state right now, but I'm checking it in to
git so that I can back it up, since I want to reinstall again.
Diffstat (limited to 'Par.hs')
| -rw-r--r-- | Par.hs | 27 |
1 files changed, 27 insertions, 0 deletions
@@ -0,0 +1,27 @@ +module Par (map, mapIO, mapIO_) where + +import Control.Parallel (par, pseq) +import Control.Concurrent (forkIO) +import qualified Control.Concurrent.MVar as MVar +import Prelude hiding (map) + +map :: (a -> b) -> [a] -> [b] +map _ [] = [] +map f (x:xs) = + let this = f x + that = Par.map f xs + in that `par` this `pseq` this : that + +mapIO :: (a -> IO b) -> [a] -> IO [b] +mapIO _ [] = return [] +mapIO f (x:xs) = do restMVar <- MVar.newEmptyMVar + _ <- forkIO (do rest <- mapIO f xs + MVar.putMVar restMVar rest) + this <- f x + rest <- MVar.takeMVar restMVar + return (this : rest) + +mapIO_ :: (a -> IO ()) -> [a] -> IO () +mapIO_ _ [] = return () +mapIO_ f (x:xs) = do _ <- forkIO (mapIO_ f xs) + f x |
