on f7e771c help sign in
streamly/examples/nested-loops.hs
22 lines 666 B history raw
1import Control.Applicative ((<|>), empty)
2import Control.Concurrent (myThreadId)
3import Control.Monad.IO.Class (liftIO)
4import System.IO (stdout, hSetBuffering, BufferMode(LineBuffering))
5import System.Random (randomIO)
6import Asyncly
8main = runAsyncly $ do
9 liftIO $ hSetBuffering stdout LineBuffering
10 x <- loop "A " 2
11 y <- loop "B " 2
12 liftIO $ myThreadId >>= putStr . show
13 >> putStr " "
14 >> print (x, y)
16 where
18 loop name n = do
19 rnd <- liftIO (randomIO :: IO Int)
20 let result = (name ++ show rnd)
21 repeat = if n > 1 then loop name (n - 1) else empty
22 in (return result) <|> repeat