on 266148c help sign in
streamly/test/nested-loops.hs
24 lines 752 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 = runStream $ do
8 yieldM $ hSetBuffering stdout LineBuffering
9 x <- loop "A " 2
10 y <- loop "B " 2
11 yieldM $ myThreadId >>= putStr . show
12 >> putStr " "
13 >> print (x, y)
15 where
17 -- we can just use
18 -- parallely $ mconcat $ replicate n $ yieldM (...)
19 loop :: String -> Int -> SerialT IO String
20 loop name n = do
21 rnd <- yieldM (randomIO :: IO Int)
22 let result = (name ++ show rnd)
23 repeat = if n > 1 then loop name (n - 1) else nil
24 in (return result) `wAsync` repeat