Skip to content

Instantly share code, notes, and snippets.

@ianoc
Last active January 2, 2016 22:08
Show Gist options
  • Save ianoc/e33b655236bafa82dd35 to your computer and use it in GitHub Desktop.
Save ianoc/e33b655236bafa82dd35 to your computer and use it in GitHub Desktop.
Reading stdin without buffering
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