Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
io-stream sample 2
{-# LANGUAGE LambdaCase #-}
module SampleStream where
import Control.Monad.IO.Class (liftIO)
import System.IO.Streams as S
main :: IO ()
main = do
i <- S.fromList [1,2,3,9,4] >>= \i' ->
fromGenerator (doNothing i') >>= \i'' ->
fromGenerator (modify i'')
o <- writeConsole
connect i o
writeConsole :: (Show a) => IO (OutputStream a)
writeConsole = S.makeOutputStream $ \case
Just x -> print x
Nothing -> return ()
doNothing :: InputStream Int -> Generator Int ()
doNothing is = do
ma <- liftIO $ S.read is
case ma of
Nothing -> return () -- stop
Just a -> do -- as it is.
yield a
doNothing is
modify :: InputStream Int -> Generator Int ()
modify is = do
ma <- liftIO $ S.read is
case ma of
Nothing -> return () -- stop
Just 9 -> return () -- stop
Just 1 -> do -- duplicate
yield 1
yield 1
modify is
Just a -> do -- as it is.
yield a
modify is
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.