Created
May 3, 2019 04:30
-
-
Save naoto-ogawa/b011355636920ffd3a23b8909ab5f664 to your computer and use it in GitHub Desktop.
io-stream sample 2
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{-# 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