Lazy file reading

@risrr451 , this seems to work:

yes | runghc lazy.hs where

-- lazy.hs
import System.IO (stdin, Handle)
import qualified Data.ByteString.Lazy as BSL
import qualified Data.ByteString.Lazy.Char8 as BSL

type ParsedLine = BSL.ByteString
parse = id

readLines :: Handle -> IO [ParsedLine]
readLines file = do
  cont <- BSL.hGetContents file
  pure (map parse (BSL.lines cont))

main = readLines stdin >>= mapM_ print

I can’t claim to understand what’s happening in your example. It has something to do with the fact that the fmap inside readLines is sequencing each and every hGetLine in the IO monad, perhaps combined with the fact that ByteString is stricter than you might expect in some cases.

For instance, my example works because I’m using Data.ByteString.Lazy.hGetContents, which specifically states that it reads the contents lazily. Contrast to Data.ByteString.hGetContents which states it reads the contents strictly.