summaryrefslogtreecommitdiffstats
path: root/Par.hs
diff options
context:
space:
mode:
authorRay Hogenson <rhogenson@posteo.net>2019-09-07 16:36:01 -0700
committerRay Hogenson <rhogenson@posteo.net>2019-09-07 16:36:01 -0700
commit440dd38f24d1df150fb5b4c7965bb6e792c929fe (patch)
tree4d9da56e839f85949dbde1f3b3b0413f3f52b753 /Par.hs
downloadevolution-main.tar.zst
Write a little guyHEADmain
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.hs27
1 files changed, 27 insertions, 0 deletions
diff --git a/Par.hs b/Par.hs
new file mode 100644
index 0000000..1465956
--- /dev/null
+++ b/Par.hs
@@ -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