2017-07-14 08:39:21 +03:00
|
|
|
import Control.Concurrent (myThreadId)
|
|
|
|
import Control.Monad.IO.Class (liftIO)
|
|
|
|
import System.IO (stdout, hSetBuffering, BufferMode(LineBuffering))
|
|
|
|
import System.Random (randomIO)
|
2017-11-20 15:33:13 +03:00
|
|
|
import Streamly
|
2018-04-17 13:15:24 +03:00
|
|
|
import Streamly.Prelude (nil)
|
2017-07-14 08:39:21 +03:00
|
|
|
|
2018-04-18 10:55:04 +03:00
|
|
|
main = runStream $ do
|
2017-07-14 08:39:21 +03:00
|
|
|
liftIO $ hSetBuffering stdout LineBuffering
|
|
|
|
x <- loop "A " 2
|
|
|
|
y <- loop "B " 2
|
|
|
|
liftIO $ myThreadId >>= putStr . show
|
|
|
|
>> putStr " "
|
|
|
|
>> print (x, y)
|
|
|
|
|
|
|
|
where
|
|
|
|
|
2018-04-17 13:15:24 +03:00
|
|
|
loop :: String -> Int -> SerialT IO String
|
2017-07-14 08:39:21 +03:00
|
|
|
loop name n = do
|
|
|
|
rnd <- liftIO (randomIO :: IO Int)
|
|
|
|
let result = (name ++ show rnd)
|
2018-04-17 13:15:24 +03:00
|
|
|
repeat = if n > 1 then loop name (n - 1) else nil
|
2018-04-17 15:19:35 +03:00
|
|
|
in (return result) `parallel` repeat
|