Fix examples to use new names

This commit is contained in:
Harendra Kumar 2018-04-17 15:45:24 +05:30
parent 583a48c490
commit dffb02fa98
7 changed files with 27 additions and 25 deletions

View File

@ -26,12 +26,12 @@ main = do
putStrLn $ "\ninterleave:\n"
runSerialT $ do
x <- return 0 <> return 1 <=> return 100 <> return 101
x <- (return 0 <> return 1) `interleave` (return 100 <> return 101)
liftIO $ print (x :: Int)
putStrLn $ "\nParallel interleave:\n"
runSerialT $ do
x <- return 0 <> return 1 <|> return 100 <> return 101
x <- (return 0 <> return 1) `parmerge` (return 100 <> return 101)
liftIO $ print (x :: Int)
where
@ -71,12 +71,12 @@ main = do
loopTailA :: Int -> SerialT IO Int
loopTailA x = do
liftIO $ putStrLn "LoopTailA..."
return x <| (if x < 3 then loopTailA (x + 1) else empty)
return x `asyncmerge` (if x < 3 then loopTailA (x + 1) else empty)
loopHeadA :: Int -> SerialT IO Int
loopHeadA x = do
liftIO $ putStrLn "LoopHeadA..."
(if x < 3 then loopHeadA (x + 1) else empty) <| return x
(if x < 3 then loopHeadA (x + 1) else empty) `asyncmerge` return x
-------------------------------------------------------------------------------
-- Parallel (fairly scheduled, multi-threaded) stream generator loops

View File

@ -1,9 +1,9 @@
import Control.Applicative ((<|>), empty)
import Control.Concurrent (myThreadId)
import Control.Monad.IO.Class (liftIO)
import System.IO (stdout, hSetBuffering, BufferMode(LineBuffering))
import System.Random (randomIO)
import Streamly
import Streamly.Prelude (nil)
main = runSerialT $ do
liftIO $ hSetBuffering stdout LineBuffering
@ -15,8 +15,9 @@ main = runSerialT $ do
where
loop :: String -> Int -> SerialT IO String
loop name n = do
rnd <- liftIO (randomIO :: IO Int)
let result = (name ++ show rnd)
repeat = if n > 1 then loop name (n - 1) else empty
in (return result) <|> repeat
repeat = if n > 1 then loop name (n - 1) else nil
in (return result) `parmerge` repeat

View File

@ -1,4 +1,3 @@
import Control.Applicative ((<|>))
import Control.Concurrent (myThreadId, threadDelay)
import Control.Monad.IO.Class (liftIO)
import System.IO (stdout, hSetBuffering, BufferMode(LineBuffering))
@ -7,14 +6,15 @@ import Streamly
main = runSerialT $ do
liftIO $ hSetBuffering stdout LineBuffering
x <- loop "A" <|> loop "B"
x <- loop "A" `parmerge` loop "B"
liftIO $ myThreadId >>= putStr . show
>> putStr " "
>> print x
where
loop :: String -> SerialT IO (String, Int)
loop name = do
liftIO $ threadDelay 1000000
rnd <- liftIO (randomIO :: IO Int)
return (name, rnd) <|> loop name
return (name, rnd) `parmerge` loop name

View File

@ -28,7 +28,7 @@ acidRain = cycle1 $ liftIO (threadDelay 1000000) >> return (Harm 1)
game :: (MonadAsync m, MonadState Int m) => SerialT m ()
game = do
event <- userAction <|> acidRain
event <- userAction `parmerge` acidRain
case event of
Harm n -> modify $ \h -> h - n
Heal n -> modify $ \h -> h + n

View File

@ -87,4 +87,4 @@ circlingSquare :: IO ()
circlingSquare = do
sdlInit
cref <- newIORef (0,0)
runSerialT $ liftIO (updateController cref) <|> liftIO (updateDisplay cref)
runSerialT $ liftIO (updateController cref) `parmerge` liftIO (updateDisplay cref)

View File

@ -1,19 +1,18 @@
{-# LANGUAGE FlexibleContexts #-}
module Streamly.Examples.ListDirRecursive where
import Control.Monad.IO.Class (liftIO)
import Path.IO (listDir, getCurrentDir)
import System.IO (stdout, hSetBuffering, BufferMode(LineBuffering))
import Streamly
import Streamly (runAsyncT)
-- | This example demonstrates that there is little difference between regular
-- IO code and concurrent streamly code. You can just remove 'runAsyncT' and
-- this is your regular IO code.
listDirRecursive :: IO ()
listDirRecursive = do
liftIO $ hSetBuffering stdout LineBuffering
runSerialT $ getCurrentDir >>= readdir
hSetBuffering stdout LineBuffering
runAsyncT $ getCurrentDir >>= readdir
where readdir d = do
(ds, fs) <- lift $ listDir d
(ds, fs) <- liftIO $ listDir d
liftIO $ mapM_ putStrLn $ map show fs ++ map show ds
--foldWith (<>) $ map readdir ds -- serial
--foldWith (<=>) $ map readdir ds -- serial interleaved
foldWith (<|) $ map readdir ds -- concurrent left biased
--foldWith (<|>) $ map readdir ds -- concurrent interleaved
foldMap readdir ds

View File

@ -7,13 +7,15 @@ import Network.HTTP.Simple
searchEngineQuery :: IO ()
searchEngineQuery = do
putStrLn "Using parallel alternative"
runSerialT $ google <|> bing <|> duckduckgo
runParallelT $ google <> bing <> duckduckgo
putStrLn "\nUsing parallel applicative zip"
runZipAsync $ (,,) <$> pure google <*> pure bing <*> pure duckduckgo
runZipAsync $ (,,) <$> google <*> bing <*> duckduckgo
where
get s = liftIO (httpNoBody (parseRequest_ s) >> putStrLn (show s))
get :: IsStream t => String -> t IO ()
google, bing, duckduckgo :: IsStream t => t IO ()
get s = adapt . serially $ liftIO (httpNoBody (parseRequest_ s) >> putStrLn (show s))
google = get "https://www.google.com/search?q=haskell"
bing = get "https://www.bing.com/search?q=haskell"
duckduckgo = get "https://www.duckduckgo.com/?q=haskell"