Skip to content

Conversation

@chris-martin
Copy link

These loops specifically address the pattern seen in Data.ByteString.hGet and Data.Text.IO.hGetChunk - these functions read a chunk of input from a handle and return an empty string when the end of the input is readed.

For example, a simple "cat" program might look like this:

import Control.Monad.Loops
import System.IO
import qualified Data.Text as T
import qualified Data.Text.IO as T

main = 
main =
  do
    [path] <- getArgs
    withFile path ReadMode $ \h ->
        forUntil_ (T.hGetChunk h) T.null $ \chunk ->
            T.putStr chunk

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant