on master help sign in
Make monoid instance append two computations in order
550eed4Harendra Kumar
src/Asyncly/AsyncT.hs+4−3
@@ -281,9 +281,10 @@ instance MonadAsync m => MonadPlus (AsyncT m) where
281 mzero = empty
281 mzero = empty
282 mplus = (<|>)
282 mplus = (<|>)
283
283
284+-- | Appends the results of two AsyncT computations in order.
284−instance (Monoid a, Monad m) => Monoid (AsyncT m a) where
285+instance MonadAsync m => Monoid (AsyncT m a) where
285− mappend x y = mappend <$> x <*> y
286− mempty = return mempty
286+ mempty = empty
287+ mappend x y = serially x y
287
288
288------------------------------------------------------------------------------
289------------------------------------------------------------------------------
289-- Num
290-- Num
test/Main.hs+3
@@ -3,6 +3,7 @@ module Main (main) where
3import Asyncly
3import Asyncly
4import Control.Applicative ((<|>))
4import Control.Applicative ((<|>))
5import Control.Concurrent (myThreadId, threadDelay)
5import Control.Concurrent (myThreadId, threadDelay)
6+import Data.Monoid ((<>))
6import Control.Monad.IO.Class (liftIO)
7import Control.Monad.IO.Class (liftIO)
7import Data.List (nub, sort)
8import Data.List (nub, sort)
8import Test.Hspec
9import Test.Hspec
@@ -23,6 +24,8 @@ main = hspec $ do
23 toList (return 1 >> return 2) `shouldReturn` ([2] :: [Int])
24 toList (return 1 >> return 2) `shouldReturn` ([2] :: [Int])
24 it "Bind and toList" $
25 it "Bind and toList" $
25 toList (do x <- return 1; y <- return 2; return (x + y)) `shouldReturn` ([3] :: [Int])
26 toList (do x <- return 1; y <- return 2; return (x + y)) `shouldReturn` ([3] :: [Int])
27+ it "Monoid composition" $
28+ toList (return 0 <> return 1) `shouldReturn` ([0, 1] :: [Int])
26 it "Alternative composition" $
29 it "Alternative composition" $
27 ((toList $ (return 0 <|> return 1)) >>= return . sort) `shouldReturn` ([0, 1] :: [Int])
30 ((toList $ (return 0 <|> return 1)) >>= return . sort) `shouldReturn` ([0, 1] :: [Int])
28 {-
31 {-