on master help sign in
Add applicative and alternative tests
commit594185f18d2860fd8bcc7c3776889b417ef9425d authorHarendra Kumar <harendra.kumar@gmail.com> dateMon 26 Jun 2017, 03:15 (9 years ago) parentb9c1c92
2 files changed+47
test/alternative.hs+30 test/applicative.hs+17
test/alternative.hsnew file+30
@@ -0,0 +1,30 @@
1+import Control.Concurrent (threadDelay, myThreadId)
2+import Control.Monad.IO.Class (liftIO)
3+import System.Random (randomIO)
4+import System.IO
5+import Control.Applicative
6+
7+import Strands
8+
9+main = wait $ threads 3 $ do
10+ liftIO $ hSetBuffering stdout LineBuffering
11+ mainThread <- liftIO myThreadId
12+ liftIO $ putStrLn $ "Main thread: " ++ show mainThread
13+
14+ eventA <|> eventB
15+
16+eventA = do
17+ x <- sample (randomIO :: IO Int) 1000000
18+ evThread <- liftIO myThreadId
19+ liftIO $ putStrLn $ "X Event thread: " ++ show evThread
20+ liftIO $ putStrLn $ "x = " ++ (show x)
21+ return x
22+
23+eventB = do
24+ y <- sample (randomIO :: IO Int) 1000000
25+
26+ -- liftIO $ threadDelay 10000000
27+ evThread <- liftIO myThreadId
28+ liftIO $ putStrLn $ "Y Event thread: " ++ show evThread
29+ liftIO $ putStrLn $ "y = " ++ (show y)
30+ return y
test/applicative.hsnew file+17
@@ -0,0 +1,17 @@
1+{-# LANGUAGE FlexibleContexts #-}
2+
3+import Control.Applicative ((<$>), (<*>), (<|>))
4+import Control.Monad.IO.Class (liftIO)
5+import System.IO
6+import Strands
7+
8+main = do
9+ hSetBuffering stdout LineBuffering
10+ xs <- gather $ threads 5 $ do
11+ x <- (,) <$> (event 1 <|> event 2) <*> (event 3 <|> event 4)
12+ --x <- (,) <$> choose [1,2] <*> choose [3,4]
13+ liftIO $ putStrLn $ show x
14+ return x
15+ putStrLn $ show xs
16+ where
17+ event n = async (do putStrLn ("event" ++ show n); return n :: IO Int)