Last active
January 2, 2016 22:08
-
-
Save ianoc/e33b655236bafa82dd35 to your computer and use it in GitHub Desktop.
Reading stdin without buffering
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
import qualified Turtle as T | |
import qualified System.IO as IO | |
import Control.DeepSeq | |
mergeUntilTwoBlankLines :: (() -> IO (Maybe String)) -> IO [String] | |
mergeUntilTwoBlankLines fn = do | |
rd <- fn () | |
processFunc [] rd | |
where processFunc ("" : existing) (Just "") = return existing | |
processFunc existing (Just nxt) = do | |
rd <- fn () | |
processFunc (nxt : existing) rd | |
processFunc existing Nothing = return existing | |
readInput :: IO [String] | |
readInput = do | |
IO.hSetBuffering IO.stdin IO.NoBuffering | |
res <- mergeUntilTwoBlankLines (\_ -> readFn) | |
res `deepseq` IO.hSetBuffering IO.stdin IO.LineBuffering | |
return res | |
where readFn = do | |
readLineMaybe <- T.readline | |
return $ fmap Text.unpack readLineMaybe |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment