on master help sign in
Fix examples
10b05b5Harendra Kumar
examples/echo-loop.hs+4−6
@@ -1,18 +1,16 @@
1import Control.Monad.IO.Class (liftIO)
1import Control.Monad.IO.Class (liftIO)
2−import Data.Monoid ((<>))
2+import Asyncly
3
3
4−import Asyncly (AsyncT, runAsyncly)
5−
6−input :: AsyncT IO String
4+input :: StreamT IO String
7input = do
5input = do
8 string <- liftIO getLine
6 string <- liftIO getLine
9 return string <> input
7 return string <> input
10
8
11−output :: AsyncT IO String -> AsyncT IO ()
9+output :: StreamT IO String -> StreamT IO ()
12output strings = do
10output strings = do
13 s <- strings
11 s <- strings
14 liftIO $ putStrLn s
12 liftIO $ putStrLn s
15
13
16main = do
14main = do
17 putStrLn "Type something and press enter, repeat"
15 putStrLn "Type something and press enter, repeat"
18− runAsyncly $ output input
16+ runStreamT $ output input
examples/loops.hs+10−10
@@ -5,32 +5,32 @@ main = do
5 liftIO $ hSetBuffering stdout LineBuffering
5 liftIO $ hSetBuffering stdout LineBuffering
6
6
7 putStrLn $ "\nloopTail:\n"
7 putStrLn $ "\nloopTail:\n"
8− runAsyncly $ do
8+ runStreamT $ do
9 x <- loopTail 0
9 x <- loopTail 0
10 liftIO $ print (x :: Int)
10 liftIO $ print (x :: Int)
11
11
12 putStrLn $ "\nloopHead:\n"
12 putStrLn $ "\nloopHead:\n"
13− runAsyncly $ do
13+ runStreamT $ do
14 x <- loopHead 0
14 x <- loopHead 0
15 liftIO $ print (x :: Int)
15 liftIO $ print (x :: Int)
16
16
17 putStrLn $ "\nloopTailA:\n"
17 putStrLn $ "\nloopTailA:\n"
18− runAsyncly $ do
18+ runStreamT $ do
19 x <- loopTailA 0
19 x <- loopTailA 0
20 liftIO $ print (x :: Int)
20 liftIO $ print (x :: Int)
21
21
22 putStrLn $ "\nloopHeadA:\n"
22 putStrLn $ "\nloopHeadA:\n"
23− runAsyncly $ do
23+ runStreamT $ do
24 x <- loopHeadA 0
24 x <- loopHeadA 0
25 liftIO $ print (x :: Int)
25 liftIO $ print (x :: Int)
26
26
27 putStrLn $ "\ninterleave:\n"
27 putStrLn $ "\ninterleave:\n"
28− runAsyncly $ do
28+ runStreamT $ do
29 x <- return 0 <> return 1 <=> return 100 <> return 101
29 x <- return 0 <> return 1 <=> return 100 <> return 101
30 liftIO $ print (x :: Int)
30 liftIO $ print (x :: Int)
31
31
32 putStrLn $ "\nParallel interleave:\n"
32 putStrLn $ "\nParallel interleave:\n"
33− runAsyncly $ do
33+ runStreamT $ do
34 x <- return 0 <> return 1 <|> return 100 <> return 101
34 x <- return 0 <> return 1 <|> return 100 <> return 101
35 liftIO $ print (x :: Int)
35 liftIO $ print (x :: Int)
36
36
@@ -46,7 +46,7 @@ main = do
46
46
47 -- Generates a value and then loops. Can be used to generate an infinite
47 -- Generates a value and then loops. Can be used to generate an infinite
48 -- stream. Interleaves the generator and the consumer.
48 -- stream. Interleaves the generator and the consumer.
49− loopTail :: Int -> AsyncT IO Int
49+ loopTail :: Int -> StreamT IO Int
50 loopTail x = do
50 loopTail x = do
51 liftIO $ putStrLn "LoopTail..."
51 liftIO $ putStrLn "LoopTail..."
52 return x <> (if x < 3 then loopTail (x + 1) else empty)
52 return x <> (if x < 3 then loopTail (x + 1) else empty)
@@ -54,7 +54,7 @@ main = do
54 -- Loops and then generates a value. The consumer can run only after the
54 -- Loops and then generates a value. The consumer can run only after the
55 -- loop has finished. An infinite generator will not let the consumer run
55 -- loop has finished. An infinite generator will not let the consumer run
56 -- at all.
56 -- at all.
57− loopHead :: Int -> AsyncT IO Int
57+ loopHead :: Int -> StreamT IO Int
58 loopHead x = do
58 loopHead x = do
59 liftIO $ putStrLn "LoopHead..."
59 liftIO $ putStrLn "LoopHead..."
60 (if x < 3 then loopHead (x + 1) else empty) <> return x
60 (if x < 3 then loopHead (x + 1) else empty) <> return x
@@ -68,12 +68,12 @@ main = do
68 -- then the action on the right is also spawned concurrently. In other
68 -- then the action on the right is also spawned concurrently. In other
69 -- words, both actions may run concurrently based on the need.
69 -- words, both actions may run concurrently based on the need.
70
70
71− loopTailA :: Int -> AsyncT IO Int
71+ loopTailA :: Int -> StreamT IO Int
72 loopTailA x = do
72 loopTailA x = do
73 liftIO $ putStrLn "LoopTailA..."
73 liftIO $ putStrLn "LoopTailA..."
74 return x <| (if x < 3 then loopTailA (x + 1) else empty)
74 return x <| (if x < 3 then loopTailA (x + 1) else empty)
75
75
76− loopHeadA :: Int -> AsyncT IO Int
76+ loopHeadA :: Int -> StreamT IO Int
77 loopHeadA x = do
77 loopHeadA x = do
78 liftIO $ putStrLn "LoopHeadA..."
78 liftIO $ putStrLn "LoopHeadA..."
79 (if x < 3 then loopHeadA (x + 1) else empty) <| return x
79 (if x < 3 then loopHeadA (x + 1) else empty) <| return x
examples/map-reduce.hs+2−1
@@ -1,6 +1,7 @@
1import Data.List (sum)
1import Data.List (sum)
2import Asyncly
2import Asyncly
3+import Asyncly.Prelude
3
4
4main = do
5main = do
5− xs <- toList $ forEachWith (<|) [1..100] $ \x -> return (x * x) :: AsyncT IO Int
6+ xs <- toList $ serially $ forEachWith (<|) [1..100] $ \x -> return (x * x)
6 print . sum $ xs
7 print . sum $ xs
examples/nested-loops.hs+1−1
@@ -5,7 +5,7 @@ import System.IO (stdout, hSetBuffering, BufferMode(LineBuffering))
5import System.Random (randomIO)
5import System.Random (randomIO)
6import Asyncly
6import Asyncly
7
7
8−main = runAsyncly $ do
8+main = runStreamT $ do
9 liftIO $ hSetBuffering stdout LineBuffering
9 liftIO $ hSetBuffering stdout LineBuffering
10 x <- loop "A " 2
10 x <- loop "A " 2
11 y <- loop "B " 2
11 y <- loop "B " 2
examples/parallel-loops.hs+1−1
@@ -5,7 +5,7 @@ import System.IO (stdout, hSetBuffering, BufferMode(LineBuffering))
5import System.Random (randomIO)
5import System.Random (randomIO)
6import Asyncly
6import Asyncly
7
7
8−main = runAsyncly $ do
8+main = runStreamT $ do
9 liftIO $ hSetBuffering stdout LineBuffering
9 liftIO $ hSetBuffering stdout LineBuffering
10 x <- loop "A" <|> loop "B"
10 x <- loop "A" <|> loop "B"
11 liftIO $ myThreadId >>= putStr . show
11 liftIO $ myThreadId >>= putStr . show
src/Asyncly/Examples/AcidRainGame.hs+4−4
@@ -13,7 +13,7 @@ import Data.Semigroup (cycle1)
13
13
14data Event = Harm Int | Heal Int | Quit deriving (Show)
14data Event = Harm Int | Heal Int | Quit deriving (Show)
15
15
16−userAction :: MonadIO m => AsyncT m Event
16+userAction :: MonadIO m => StreamT m Event
17userAction = cycle1 $ liftIO askUser
17userAction = cycle1 $ liftIO askUser
18 where
18 where
19 askUser = do
19 askUser = do
@@ -23,10 +23,10 @@ userAction = cycle1 $ liftIO askUser
23 "quit" -> return Quit
23 "quit" -> return Quit
24 _ -> putStrLn "What?" >> askUser
24 _ -> putStrLn "What?" >> askUser
25
25
26−acidRain :: MonadIO m => AsyncT m Event
26+acidRain :: MonadIO m => StreamT m Event
27acidRain = cycle1 $ liftIO (threadDelay 1000000) >> return (Harm 1)
27acidRain = cycle1 $ liftIO (threadDelay 1000000) >> return (Harm 1)
28
28
29−game :: (MonadAsync m, MonadState Int m) => AsyncT m ()
29+game :: (MonadAsync m, MonadState Int m) => StreamT m ()
30game = do
30game = do
31 event <- userAction <|> acidRain
31 event <- userAction <|> acidRain
32 case event of
32 case event of
@@ -42,5 +42,5 @@ acidRainGame :: IO ()
42acidRainGame = do
42acidRainGame = do
43 putStrLn "Your health is deteriorating due to acid rain,\
43 putStrLn "Your health is deteriorating due to acid rain,\
44 \ type \"potion\" or \"quit\""
44 \ type \"potion\" or \"quit\""
45− _ <- runStateT (runAsyncly game) 60
45+ _ <- runStateT (runStreamT game) 60
46 return ()
46 return ()
src/Asyncly/Examples/CirclingSquare.hs+1−1
@@ -87,4 +87,4 @@ circlingSquare :: IO ()
87circlingSquare = do
87circlingSquare = do
88 sdlInit
88 sdlInit
89 cref <- newIORef (0,0)
89 cref <- newIORef (0,0)
90− runAsyncly $ liftIO (updateController cref) <|> liftIO (updateDisplay cref)
90+ runStreamT $ liftIO (updateController cref) <|> liftIO (updateDisplay cref)
src/Asyncly/Examples/ListDirRecursive.hs+1−1
@@ -9,7 +9,7 @@ import Asyncly
9listDirRecursive :: IO ()
9listDirRecursive :: IO ()
10listDirRecursive = do
10listDirRecursive = do
11 liftIO $ hSetBuffering stdout LineBuffering
11 liftIO $ hSetBuffering stdout LineBuffering
12− runAsyncly $ getCurrentDir >>= readdir
12+ runStreamT $ getCurrentDir >>= readdir
13 where readdir d = do
13 where readdir d = do
14 (ds, fs) <- lift $ listDir d
14 (ds, fs) <- lift $ listDir d
15 liftIO $ mapM_ putStrLn $ map show fs ++ map show ds
15 liftIO $ mapM_ putStrLn $ map show fs ++ map show ds
src/Asyncly/Examples/MergeSortedStreams.hs+5−4
@@ -8,19 +8,20 @@ import Data.List (sort)
8import Asyncly
8import Asyncly
9import qualified Asyncly.Prelude as A
9import qualified Asyncly.Prelude as A
10
10
11−getSorted :: MonadIO m => AsyncT m Word16
11+getSorted :: MonadIO m => StreamT m Word16
12getSorted = do
12getSorted = do
13 g <- liftIO getStdGen
13 g <- liftIO getStdGen
14 let ls = take 100000 (randoms g) :: [Word16]
14 let ls = take 100000 (randoms g) :: [Word16]
15 foldMapWith (<>) return (sort ls)
15 foldMapWith (<>) return (sort ls)
16
16
17−mergeAsync :: (Ord a, MonadAsync m) => AsyncT m a -> AsyncT m a -> AsyncT m a
17+mergeAsync :: (Ord a, MonadAsync m)
18+ => StreamT m a -> StreamT m a -> StreamT m a
18mergeAsync a b = do
19mergeAsync a b = do
19 x <- lift $ async a
20 x <- lift $ async a
20 y <- lift $ async b
21 y <- lift $ async b
21 merge x y
22 merge x y
22
23
23−merge :: (Ord a, MonadAsync m) => AsyncT m a -> AsyncT m a -> AsyncT m a
24+merge :: (Ord a, MonadAsync m) => StreamT m a -> StreamT m a -> StreamT m a
24merge a b = do
25merge a b = do
25 a1 <- lift $ A.uncons a
26 a1 <- lift $ A.uncons a
26 case a1 of
27 case a1 of
@@ -36,5 +37,5 @@ merge a b = do
36
37
37mergeSortedStreams :: IO ()
38mergeSortedStreams :: IO ()
38mergeSortedStreams = do
39mergeSortedStreams = do
39− xs <- toList $ mergeAsync getSorted getSorted
40+ xs <- A.toList $ mergeAsync getSorted getSorted
40 putStrLn $ show $ length xs
41 putStrLn $ show $ length xs
src/Asyncly/Examples/SearchEngineQuery.hs+2−4
@@ -1,18 +1,16 @@
1module Asyncly.Examples.SearchEngineQuery where
1module Asyncly.Examples.SearchEngineQuery where
2
2
3import Asyncly
3import Asyncly
4−import Asyncly.Prelude (ZipAsync (..))
5import Network.HTTP.Simple
4import Network.HTTP.Simple
6
5
7-- Runs three search engine queries in parallel.
6-- Runs three search engine queries in parallel.
8searchEngineQuery :: IO ()
7searchEngineQuery :: IO ()
9searchEngineQuery = do
8searchEngineQuery = do
10 putStrLn "Using parallel alternative"
9 putStrLn "Using parallel alternative"
11− runAsyncly $ google <|> bing <|> duckduckgo
10+ runStreamT $ google <|> bing <|> duckduckgo
12
11
13 putStrLn "\nUsing parallel applicative zip"
12 putStrLn "\nUsing parallel applicative zip"
14− runAsyncly $ getZipAsync $
15− (,,) <$> ZipAsync google <*> ZipAsync bing <*> ZipAsync duckduckgo
13+ runZipAsync $ (,,) <$> pure google <*> pure bing <*> pure duckduckgo
16
14
17 where
15 where
18 get s = liftIO (httpNoBody (parseRequest_ s) >> putStrLn (show s))
16 get s = liftIO (httpNoBody (parseRequest_ s) >> putStrLn (show s))