on 57fad21 help sign in
streamly/test/nested-loops.hs
25 lines 766 B history raw
1import Control.Concurrent (myThreadId)
2import System.IO (stdout, hSetBuffering, BufferMode(LineBuffering))
3import System.Random (randomIO)
4import Streamly
5import Streamly.Prelude (nil, yieldM)
7main :: IO ()
8main = runStream $ do
9 yieldM $ hSetBuffering stdout LineBuffering
10 x <- loop "A " 2
11 y <- loop "B " 2
12 yieldM $ myThreadId >>= putStr . show
13 >> putStr " "
14 >> print (x, y)
16 where
18 -- we can just use
19 -- parallely $ mconcat $ replicate n $ yieldM (...)
20 loop :: String -> Int -> SerialT IO String
21 loop name n = do
22 rnd <- yieldM (randomIO :: IO Int)
23 let result = name <> show rnd
24 repeatIt = if n > 1 then loop name (n - 1) else nil
25 in return result `wAsync` repeatIt