@@ -1,21 +1,21 @@
11module Main (main) where
22
3−import Asyncly
43import Control.Applicative ((<|>), empty)
54import Control.Monad (mzero)
6−import Control.Concurrent (myThreadId, threadDelay)
7−import Data.Monoid ((<>))
5+-- import Control.Concurrent (myThreadId, threadDelay)
86import Control.Monad.IO.Class (liftIO)
9−import Data.List (nub, sort)
7+-- import Data.List (nub)
8+import Data.List (sort)
9+import Data.Monoid ((<>))
1010import Test.Hspec
1111
12−default (Int)
12+import Asyncly
1313
1414main :: IO ()
1515main = hspec $ do
1616 describe "Runners" $ do
1717 it "simple runAsyncly" $
18− runAsyncly (return 0) `shouldReturn` ()
18+ runAsyncly (return (0 :: Int)) `shouldReturn` ()
1919 it "simple runAsyncly with IO" $
2020 runAsyncly (liftIO $ putStrLn "hello") `shouldReturn` ()
2121 it "Captures a return value using toList" $
@@ -41,65 +41,74 @@ main = hspec $ do
4141 describe "Left Biased parallel loops (<|)" $ loops (<|) sort sort
4242 describe "Fair parallel loops (<|>)" $ loops (<|>) sort sort
4343
44+bind :: Spec
4445bind = do
4546 it "Simple runAsyncly and 'then' with IO" $
4647 runAsyncly (liftIO (putStrLn "hello") >> liftIO (putStrLn "world"))
4748 `shouldReturn` ()
4849 it "Then and toList" $
49− toList (return 1 >> return 2) `shouldReturn` ([2] :: [Int])
50+ toList (return (1 :: Int) >> return 2) `shouldReturn` ([2] :: [Int])
5051 it "Bind and toList" $
5152 toList (do x <- return 1; y <- return 2; return (x + y))
5253 `shouldReturn` ([3] :: [Int])
5354
55+interleaved :: (AsyncT IO Int -> AsyncT IO Int -> AsyncT IO Int) -> Spec
5456interleaved f =
5557 it "Interleave four" $
56− toList ((return (0 :: Int) <> return 1) `f` (return 100 <> return 101))
58+ toList ((return 0 <> return 1) `f` (return 100 <> return 101))
5759 `shouldReturn` ([0, 100, 1, 101])
5860
61+compose
62+ :: (AsyncT IO Int -> AsyncT IO Int -> AsyncT IO Int)
63+ -> ([Int] -> [Int])
64+ -> Spec
5965compose f srt = do
6066 it "Compose mempty, mempty" $
61− (toList (mempty `f` mempty)) `shouldReturn` ([] :: [Int])
67+ (toList (mempty `f` mempty)) `shouldReturn` []
6268 it "Compose empty, empty" $
63− (toList (empty `f` empty)) `shouldReturn` ([] :: [Int])
69+ (toList (empty `f` empty)) `shouldReturn` []
6470 it "Compose empty at the beginning" $
65− (toList $ (empty `f` return 1)) `shouldReturn` ([1] :: [Int])
71+ (toList $ (empty `f` return 1)) `shouldReturn` [1]
6672 it "Compose empty at the end" $
67− (toList $ (return 1 `f` empty)) `shouldReturn` ([1] :: [Int])
73+ (toList $ (return 1 `f` empty)) `shouldReturn` [1]
6874 it "Compose two" $
6975 (toList (return 0 `f` return 1) >>= return . srt)
70− `shouldReturn` ([0, 1] :: [Int])
76+ `shouldReturn` [0, 1]
7177 it "Compose three - empty in the middle" $
7278 ((toList $ (return 0 `f` empty `f` return 1)) >>= return . srt)
73− `shouldReturn` ([0, 1] :: [Int])
79+ `shouldReturn` [0, 1]
7480 it "Compose left associated" $
7581 ((toList $ (((return 0 `f` return 1) `f` return 2) `f` return 3))
76− >>= return . srt) `shouldReturn` ([0, 1, 2, 3] :: [Int])
82+ >>= return . srt) `shouldReturn` [0, 1, 2, 3]
7783 it "Compose right associated" $
7884 ((toList $ (return 0 `f` (return 1 `f` (return 2 `f` return 3))))
79− >>= return . srt) `shouldReturn` ([0, 1, 2, 3] :: [Int])
85+ >>= return . srt) `shouldReturn` [0, 1, 2, 3]
8086 it "Compose many" $
8187 ((toList $ forEachWith f [1..100] return) >>= return . srt)
82− `shouldReturn` ([1..100] :: [Int])
88+ `shouldReturn` [1..100]
8389 it "Compose many (right fold) with bind" $
8490 (toList (forEachWith f [1..10 :: Int] $ \x -> return x >>= return . id)
85− >>= return . srt) `shouldReturn` ([1..10] :: [Int])
91+ >>= return . srt) `shouldReturn` [1..10]
8692 it "Compose many (left fold) with bind" $
8793 let forL xs k = foldl f empty $ map k xs
8894 in (toList (forL [1..10 :: Int] $ \x -> return x >>= return . id)
89− >>= return . srt) `shouldReturn` ([1..10] :: [Int])
95+ >>= return . srt) `shouldReturn` [1..10]
9096 it "Compose hierarchical (multiple levels)" $
9197 ((toList $ (((return 0 `f` return 1) `f` (return 2 `f` return 3))
9298 `f` ((return 4 `f` return 5) `f` (return 6 `f` return 7)))
93− ) >>= return . srt) `shouldReturn` ([0..7] :: [Int])
99+ ) >>= return . srt) `shouldReturn` [0..7]
94100
101+loops
102+ :: (AsyncT IO Int -> AsyncT IO Int -> AsyncT IO Int)
103+ -> ([Int] -> [Int])
104+ -> ([Int] -> [Int])
105+ -> Spec
95106loops f tsrt hsrt = do
96− it "Tail recursive loop" $
97− (toList (loopTail (0 :: Int)) >>= return . tsrt)
98− `shouldReturn` ([0..3] :: [Int])
107+ it "Tail recursive loop" $ (toList (loopTail 0) >>= return . tsrt)
108+ `shouldReturn` [0..3]
99109
100− it "Head recursive loop" $
101− (toList (loopHead (0 :: Int)) >>= return . hsrt)
102− `shouldReturn` ([0..3] :: [Int])
110+ it "Head recursive loop" $ (toList (loopHead 0) >>= return . hsrt)
111+ `shouldReturn` [0..3]
103112
104113 where
105114 loopHead x = do