on master help sign in
update examples
commitcef2f7da14a9f1780acf7d00feed46238c9ad698 authorHarendra Kumar <harendra.kumar@gmail.com> dateTue 17 Oct 2017, 02:48 (9 years ago) parentf69eb64
5 files changed+13−90
examples/alternative.hs−31 examples/applicative.hs−17 examples/fastest.hs+13−8 examples/logged.hs−34 examples/loop.hs → examples/loops.hs
examples/alternative.hsdeleted−31
@@ -1,31 +0,0 @@
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 Asyncly
8−
9−main = runAsyncly $ 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− liftIO $ threadDelay 1000000
18− x <- liftIO (randomIO :: IO Int)
19− evThread <- liftIO myThreadId
20− liftIO $ putStrLn $ "A Event thread: " ++ show evThread
21− liftIO $ putStrLn $ "a = " ++ (show x)
22− return x
23−
24−eventB = do
25− y <- liftIO (randomIO :: IO Int)
26−
27− -- liftIO $ threadDelay 10000000
28− evThread <- liftIO myThreadId
29− liftIO $ putStrLn $ "B Event thread: " ++ show evThread
30− liftIO $ putStrLn $ "b = " ++ (show y)
31− return y
examples/applicative.hsdeleted−17
@@ -1,17 +0,0 @@
1−{-# LANGUAGE FlexibleContexts #-}
2−
3−import Control.Applicative ((<$>), (<*>), (<|>))
4−import Control.Monad.IO.Class (liftIO)
5−import System.IO
6−import Asyncly
7−
8−main = do
9− hSetBuffering stdout LineBuffering
10− xs <- runAsyncly $ do
11− x <- (,) <$> (event 1 <|> event 2) <*> (event 3 <|> event 4)
12− --x <- (,) <$> (for [1,2] return) <*> (for [3,4] return)
13− liftIO $ putStrLn $ show x
14− return x
15− putStrLn $ show xs
16− where
17− event n = liftIO $ (do putStrLn ("event" ++ show n); return n :: IO Int)
examples/fastest.hs+13−8
@@ -4,12 +4,17 @@
44import Asyncly
55import Network.HTTP.Simple
66
7+-- Runs three search engine queries in parallel.
78main = do
8− let urls = [ "https://www.google.com/search?q=haskell"
9− , "https://www.duckduckgo.com/?q=haskell"
10− , "https://www.bing.com/search?q=haskell"
11− ]
12− xs <- toList $ foldWith (<|>) $ map get urls
13− mapM_ (putStrLn . show) $ zip [1..] xs
14−
15− where get s = liftIO $ httpNoBody (parseRequest_ s) >> return s
9+ putStrLn "Using parallel alternative"
10+ runAsyncly $ google <|> bing <|> duckduckgo
11+
12+ putStrLn "\nUsing parallel applicative zip"
13+ runAsyncly $ getZipAsync $
14+ (,,) <$> ZipAsync google <*> ZipAsync bing <*> ZipAsync duckduckgo
15+
16+ where
17+ get s = liftIO (httpNoBody (parseRequest_ s) >> putStrLn (show s))
18+ google = get "https://www.google.com/search?q=haskell"
19+ bing = get "https://www.bing.com/search?q=haskell"
20+ duckduckgo = get "https://www.duckduckgo.com/?q=haskell"
examples/logged.hsdeleted−34
@@ -1,34 +0,0 @@
1−{-# LANGUAGE FlexibleContexts #-}
2−
3−import Control.Applicative ((<$>), (<*>), (<|>))
4−import Control.Monad.IO.Class (liftIO)
5−import System.IO
6−import Asyncly
7−import Control.Monad.Trans.Recorder (pause, record)
8−
9−main = do
10− hSetBuffering stdout LineBuffering
11− logs <- runAsynclyRecorded comp
12−
13− putStrLn $ "\nResuming with logs:"
14− logs1 <- runAsynclyRecorded $ playRecordings comp logs
15−
16− putStrLn $ "\nResuming with logs:"
17− logs2 <- runAsynclyRecorded $ playRecordings comp logs1
18−
19− putStrLn $ "\nLogs at the end must be empty:"
20− putStrLn $ show logs2
21−
22− where
23−
24− comp = record $ threads 5 $ do
25− r <- record $ each [1..3]
26− record $ liftIO $ print ("A",r)
27− record pause
28− record $ liftIO $ print ("B",r)
29− x <- record $ (,) <$> (event 1 <|> event 2) <*> (event 3 <|> event 4)
30− record pause
31− liftIO $ print ("C", r, x)
32−
33− -- event n = async $ liftIO $ (do putStrLn ("event" ++ show n); return n :: IO Int)
34− event n = liftIO $ (do putStrLn ("event" ++ show n); return n :: IO Int)
examples/loop.hs → examples/loops.hsrenamed

The content is the same.