Buscar..


Streaming IO

io-streams es una biblioteca basada en Stream que se centra en la abstracción de Stream, pero para IO. Expone dos tipos:

  • InputStream : un controlador inteligente de solo lectura

  • OutputStream : un controlador inteligente de solo escritura

Podemos crear un flujo con makeInputStream :: IO (Maybe a) -> IO (InputStream a) . La lectura de un flujo se realiza usando read :: InputStream a -> IO (Maybe a) , donde Nothing denota un EOF:

import Control.Monad (forever)
import qualified System.IO.Streams as S
import System.Random (randomRIO)

main :: IO ()
main = do
  is <- S.makeInputStream $ randomInt  -- create an InputStream
  forever $ printStream =<< S.read is  -- forever read from that stream
  return ()

randomInt :: IO (Maybe Int)
randomInt = do
  r <- randomRIO (1, 100)
  return $ Just r

printStream :: Maybe Int -> IO ()
printStream Nothing  = print "Nada!"
printStream (Just a) = putStrLn $ show a



Modified text is an extract of the original Stack Overflow Documentation
Licenciado bajo CC BY-SA 3.0
No afiliado a Stack Overflow