Skip to content

Instantly share code, notes, and snippets.

@ttuegel
Last active August 29, 2015 14:01
Show Gist options
  • Save ttuegel/61af02ec2e1b86709bc1 to your computer and use it in GitHub Desktop.
Save ttuegel/61af02ec2e1b86709bc1 to your computer and use it in GitHub Desktop.
Strict implementation of `tee`
module Tee where
import Control.Concurrent (forkIO)
import Control.Exception (handle, throw)
import Control.Monad (forever, void)
import System.IO (Handle, hGetChar, hPutChar)
import System.IO.Error (isEOFError)
-- This implementation is actually worse than the lazy one! The lazy
-- implementation would lock up if all of the output handles became full;
-- this one locks up if *any* of the output handles become full.
tee :: Handle -> [Handle] -> IO () -> IO ()
tee inH outHs finalAct =
void $ forkIO $ handleEOF $ forever $ do
c <- hGetChar inH
mapM_ (flip hPutChar c) outHs
where
handleEOF = handle $ \e -> if isEOFError e then finalAct else throw e
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment