on master help sign in
Fix examples
commit10b05b545e5c3ced0cfb445399ca5775af719352 authorHarendra Kumar <harendra.kumar@gmail.com> dateThu 26 Oct 2017, 13:06 (9 years ago) parent1b88113
10 files changed+31−33
examples/echo-loop.hs+4−6 examples/loops.hs+10−10 examples/map-reduce.hs+2−1 examples/nested-loops.hs+1−1 examples/parallel-loops.hs+1−1 src/Asyncly/Examples/AcidRainGame.hs+4−4 src/Asyncly/Examples/CirclingSquare.hs+1−1 src/Asyncly/Examples/ListDirRecursive.hs+1−1 src/Asyncly/Examples/MergeSortedStreams.hs+5−4 src/Asyncly/Examples/SearchEngineQuery.hs+2−4
examples/echo-loop.hs+4−6
@@ -1,18 +1,16 @@
11import Control.Monad.IO.Class (liftIO)
2−import Data.Monoid ((<>))
2+import Asyncly
33
4−import Asyncly (AsyncT, runAsyncly)
5−
6−input :: AsyncT IO String
4+input :: StreamT IO String
75input = do
86 string <- liftIO getLine
97 return string <> input
108
11−output :: AsyncT IO String -> AsyncT IO ()
9+output :: StreamT IO String -> StreamT IO ()
1210output strings = do
1311 s <- strings
1412 liftIO $ putStrLn s
1513
1614main = do
1715 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
55 liftIO $ hSetBuffering stdout LineBuffering
66
77 putStrLn $ "\nloopTail:\n"
8− runAsyncly $ do
8+ runStreamT $ do
99 x <- loopTail 0
1010 liftIO $ print (x :: Int)
1111
1212 putStrLn $ "\nloopHead:\n"
13− runAsyncly $ do
13+ runStreamT $ do
1414 x <- loopHead 0
1515 liftIO $ print (x :: Int)
1616
1717 putStrLn $ "\nloopTailA:\n"
18− runAsyncly $ do
18+ runStreamT $ do
1919 x <- loopTailA 0
2020 liftIO $ print (x :: Int)
2121
2222 putStrLn $ "\nloopHeadA:\n"
23− runAsyncly $ do
23+ runStreamT $ do
2424 x <- loopHeadA 0
2525 liftIO $ print (x :: Int)
2626
2727 putStrLn $ "\ninterleave:\n"
28− runAsyncly $ do
28+ runStreamT $ do
2929 x <- return 0 <> return 1 <=> return 100 <> return 101
3030 liftIO $ print (x :: Int)
3131
3232 putStrLn $ "\nParallel interleave:\n"
33− runAsyncly $ do
33+ runStreamT $ do
3434 x <- return 0 <> return 1 <|> return 100 <> return 101
3535 liftIO $ print (x :: Int)
3636
@@ -46,7 +46,7 @@ main = do
4646
4747 -- Generates a value and then loops. Can be used to generate an infinite
4848 -- stream. Interleaves the generator and the consumer.
49− loopTail :: Int -> AsyncT IO Int
49+ loopTail :: Int -> StreamT IO Int
5050 loopTail x = do
5151 liftIO $ putStrLn "LoopTail..."
5252 return x <> (if x < 3 then loopTail (x + 1) else empty)
@@ -54,7 +54,7 @@ main = do
5454 -- Loops and then generates a value. The consumer can run only after the
5555 -- loop has finished. An infinite generator will not let the consumer run
5656 -- at all.
57− loopHead :: Int -> AsyncT IO Int
57+ loopHead :: Int -> StreamT IO Int
5858 loopHead x = do
5959 liftIO $ putStrLn "LoopHead..."
6060 (if x < 3 then loopHead (x + 1) else empty) <> return x
@@ -68,12 +68,12 @@ main = do
6868 -- then the action on the right is also spawned concurrently. In other
6969 -- words, both actions may run concurrently based on the need.
7070
71− loopTailA :: Int -> AsyncT IO Int
71+ loopTailA :: Int -> StreamT IO Int
7272 loopTailA x = do
7373 liftIO $ putStrLn "LoopTailA..."
7474 return x <| (if x < 3 then loopTailA (x + 1) else empty)
7575
76− loopHeadA :: Int -> AsyncT IO Int
76+ loopHeadA :: Int -> StreamT IO Int
7777 loopHeadA x = do
7878 liftIO $ putStrLn "LoopHeadA..."
7979 (if x < 3 then loopHeadA (x + 1) else empty) <| return x
examples/map-reduce.hs+2−1
@@ -1,6 +1,7 @@
11import Data.List (sum)
22import Asyncly
3+import Asyncly.Prelude
34
45main = 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)
67 print . sum $ xs
examples/nested-loops.hs+1−1
@@ -5,7 +5,7 @@ import System.IO (stdout, hSetBuffering, BufferMode(LineBuffering))
55import System.Random (randomIO)
66import Asyncly
77
8−main = runAsyncly $ do
8+main = runStreamT $ do
99 liftIO $ hSetBuffering stdout LineBuffering
1010 x <- loop "A " 2
1111 y <- loop "B " 2
examples/parallel-loops.hs+1−1
@@ -5,7 +5,7 @@ import System.IO (stdout, hSetBuffering, BufferMode(LineBuffering))
55import System.Random (randomIO)
66import Asyncly
77
8−main = runAsyncly $ do
8+main = runStreamT $ do
99 liftIO $ hSetBuffering stdout LineBuffering
1010 x <- loop "A" <|> loop "B"
1111 liftIO $ myThreadId >>= putStr . show
src/Asyncly/Examples/AcidRainGame.hs+4−4
@@ -13,7 +13,7 @@ import Data.Semigroup (cycle1)
1313
1414data Event = Harm Int | Heal Int | Quit deriving (Show)
1515
16−userAction :: MonadIO m => AsyncT m Event
16+userAction :: MonadIO m => StreamT m Event
1717userAction = cycle1 $ liftIO askUser
1818 where
1919 askUser = do
@@ -23,10 +23,10 @@ userAction = cycle1 $ liftIO askUser
2323 "quit" -> return Quit
2424 _ -> putStrLn "What?" >> askUser
2525
26−acidRain :: MonadIO m => AsyncT m Event
26+acidRain :: MonadIO m => StreamT m Event
2727acidRain = cycle1 $ liftIO (threadDelay 1000000) >> return (Harm 1)
2828
29−game :: (MonadAsync m, MonadState Int m) => AsyncT m ()
29+game :: (MonadAsync m, MonadState Int m) => StreamT m ()
3030game = do
3131 event <- userAction <|> acidRain
3232 case event of
@@ -42,5 +42,5 @@ acidRainGame :: IO ()
4242acidRainGame = do
4343 putStrLn "Your health is deteriorating due to acid rain,\
4444 \ type \"potion\" or \"quit\""
45− _ <- runStateT (runAsyncly game) 60
45+ _ <- runStateT (runStreamT game) 60
4646 return ()
src/Asyncly/Examples/CirclingSquare.hs+1−1
@@ -87,4 +87,4 @@ circlingSquare :: IO ()
8787circlingSquare = do
8888 sdlInit
8989 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
99listDirRecursive :: IO ()
1010listDirRecursive = do
1111 liftIO $ hSetBuffering stdout LineBuffering
12− runAsyncly $ getCurrentDir >>= readdir
12+ runStreamT $ getCurrentDir >>= readdir
1313 where readdir d = do
1414 (ds, fs) <- lift $ listDir d
1515 liftIO $ mapM_ putStrLn $ map show fs ++ map show ds
src/Asyncly/Examples/MergeSortedStreams.hs+5−4
@@ -8,19 +8,20 @@ import Data.List (sort)
88import Asyncly
99import qualified Asyncly.Prelude as A
1010
11−getSorted :: MonadIO m => AsyncT m Word16
11+getSorted :: MonadIO m => StreamT m Word16
1212getSorted = do
1313 g <- liftIO getStdGen
1414 let ls = take 100000 (randoms g) :: [Word16]
1515 foldMapWith (<>) return (sort ls)
1616
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
1819mergeAsync a b = do
1920 x <- lift $ async a
2021 y <- lift $ async b
2122 merge x y
2223
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
2425merge a b = do
2526 a1 <- lift $ A.uncons a
2627 case a1 of
@@ -36,5 +37,5 @@ merge a b = do
3637
3738mergeSortedStreams :: IO ()
3839mergeSortedStreams = do
39− xs <- toList $ mergeAsync getSorted getSorted
40+ xs <- A.toList $ mergeAsync getSorted getSorted
4041 putStrLn $ show $ length xs
src/Asyncly/Examples/SearchEngineQuery.hs+2−4
@@ -1,18 +1,16 @@
11module Asyncly.Examples.SearchEngineQuery where
22
33import Asyncly
4−import Asyncly.Prelude (ZipAsync (..))
54import Network.HTTP.Simple
65
76-- Runs three search engine queries in parallel.
87searchEngineQuery :: IO ()
98searchEngineQuery = do
109 putStrLn "Using parallel alternative"
11− runAsyncly $ google <|> bing <|> duckduckgo
10+ runStreamT $ google <|> bing <|> duckduckgo
1211
1312 putStrLn "\nUsing parallel applicative zip"
14− runAsyncly $ getZipAsync $
15− (,,) <$> ZipAsync google <*> ZipAsync bing <*> ZipAsync duckduckgo
13+ runZipAsync $ (,,) <$> pure google <*> pure bing <*> pure duckduckgo
1614
1715 where
1816 get s = liftIO (httpNoBody (parseRequest_ s) >> putStrLn (show s))