streamly/examples/nested-loops.hs

24 lines
706 B
Haskell
Raw Normal View History

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)
import Streamly
2018-04-17 13:15:24 +03:00
import Streamly.Prelude (nil)
2017-07-14 08:39:21 +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
in (return result) `parallel` repeat