Last active
August 29, 2015 14:01
-
-
Save ttuegel/61af02ec2e1b86709bc1 to your computer and use it in GitHub Desktop.
Strict implementation of `tee`
This file contains 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
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