From d4f2fec43f9fb5db355a39a074dd0051394ddcc9 Mon Sep 17 00:00:00 2001 From: Harendra Kumar Date: Mon, 26 Jun 2017 02:21:53 +0530 Subject: [PATCH] Rename the monad runner to 'gather' --- src/Strands.hs | 2 +- src/Strands/Threads.hs | 15 ++++++++------- test/{2-waitAsync.hs => 2-gather.hs} | 2 +- test/3-async.hs | 2 +- 4 files changed, 11 insertions(+), 10 deletions(-) rename test/{2-waitAsync.hs => 2-gather.hs} (83%) diff --git a/src/Strands.hs b/src/Strands.hs index 8c37893c9..e5f89894c 100644 --- a/src/Strands.hs +++ b/src/Strands.hs @@ -9,7 +9,7 @@ -- module Strands - ( waitAsync + ( gather , async , sample , threads diff --git a/src/Strands/Threads.hs b/src/Strands/Threads.hs index f72909032..fa262a86d 100644 --- a/src/Strands/Threads.hs +++ b/src/Strands/Threads.hs @@ -9,7 +9,7 @@ module Strands.Threads , sync --, react , threads - , waitAsync + , gather ) where @@ -538,13 +538,14 @@ finishComputation x = AsyncT $ do collectResult x return Nothing --- XXX pass a collector function and return a Traversable. --- XXX Ideally it should be a non-empty list instead. --- | Run an 'AsyncT m' computation. Returns a list of results of the --- computation or may throw an exception. -waitAsync :: forall m a. (MonadIO m, MonadCatch m) +-- XXX pass a collector function and return a Traversable? +-- XXX Ideally it should be a non-empty list. + +-- | Run an 'AsyncT m' computation and collect the results generated by each +-- thread of the computation in a list. +gather :: forall m a. (MonadIO m, MonadCatch m) => AsyncT m a -> m [a] -waitAsync m = do +gather m = do childChan <- liftIO $ atomically newTChan pendingRef <- liftIO $ newIORef [] resultsRef <- liftIO $ newIORef [] diff --git a/test/2-waitAsync.hs b/test/2-gather.hs similarity index 83% rename from test/2-waitAsync.hs rename to test/2-gather.hs index 0e03c6851..df7de5964 100644 --- a/test/2-waitAsync.hs +++ b/test/2-gather.hs @@ -2,7 +2,7 @@ import Strands import Control.Monad.IO.Class (liftIO) main = do - xs <- waitAsync $ do + xs <- gather $ do liftIO $ putStrLn "hello" return 5 print xs diff --git a/test/3-async.hs b/test/3-async.hs index ddcbe1d1c..3eb6dd17e 100644 --- a/test/3-async.hs +++ b/test/3-async.hs @@ -7,7 +7,7 @@ import Strands main = do hSetBuffering stdout LineBuffering - xs <- waitAsync $ threads 4 $ do + xs <- gather $ threads 4 $ do liftIO $ hSetBuffering stdout LineBuffering mainThread <- liftIO myThreadId liftIO $ putStrLn $ "Main thread: " ++ show mainThread