streamly/examples/nested-loops.hs
Harendra Kumar a74a0c0844 Rename to Streamly
Streamly is a more appropriate name and conveys the core idea. Stream
concurrently = streamly.
2017-11-20 18:03:13 +05:30

23 lines
667 B
Haskell

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
main = runStreamT $ do
liftIO $ hSetBuffering stdout LineBuffering
x <- loop "A " 2
y <- loop "B " 2
liftIO $ myThreadId >>= putStr . show
>> putStr " "
>> print (x, y)
where
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