Skip to content

Instantly share code, notes, and snippets.

@wraithm
Last active August 29, 2015 14:16
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wraithm/74f5f1efad61c838575d to your computer and use it in GitHub Desktop.
Save wraithm/74f5f1efad61c838575d to your computer and use it in GitHub Desktop.
Bifurcate io-streams example
{-# LANGUAGE OverloadedStrings #-}
module Main where
import Control.Monad (when)
import Data.Maybe (isJust)
import Data.ByteString.Char8 as C
import System.IO.Streams as Streams
infListStream :: IO (InputStream Int)
infListStream = Streams.fromList [1..10]
bifurcate :: InputStream a -> OutputStream a -> OutputStream a -> IO ()
bifurcate is os1 os2 = do
x <- Streams.read is
Streams.write x os1
Streams.write x os2
when (isJust x) $ bifurcate is os1 os2
echo :: IO (OutputStream ByteString, InputStream ByteString)
echo = do
(inp, outp, _, _) <- Streams.runInteractiveProcess "/usr/bin/cat" [ "/dev/stdin" ] Nothing Nothing
return (inp, outp)
echo2 :: IO (OutputStream ByteString, InputStream ByteString)
echo2 = do
(inp, outp, _, _) <- Streams.runInteractiveProcess "/usr/bin/cat" [ "/dev/stdin" ] Nothing Nothing
inp2 <- Streams.intersperse " " inp
return (inp2, outp)
main :: IO ()
main = do
xs <- infListStream
ys <- Streams.map (C.pack . show) xs
(i1, o1) <- echo
(i2, o2) <- echo2
bifurcate ys i1 i2
x <- Streams.read o1
y <- Streams.read o2
print (x, y)
---- Output
-- (Just "12345678910",Just "1 2 3 4 5 6 7 8 9 10")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment