haskelltextlazy-io

`interact` using Text instead of String


I'd like to rewrite the interact function, but using Text instead of String. Is it possible to use Data.Text and/or Data.Text.Lazy to accomplish the same behavior as interact?

For example, when I run this program using String:

main = interact (unlines . map f . lines)
  where f "hello" = "wassup"
        f _ = "wat?"

it waits for a line of input, and then prints out a line in response, and waits for the next line of input. I'd like to write the same code and have it work for Text.

{-# LANGUAGE OverloadedStrings #-}
import Data.Text.Lazy (Text)
import qualified Data.Text.Lazy as T

textInteract :: (Text -> Text) -> IO ()
textInteract = undefined

main = textInteract (T.unlines . map f . T.lines)
  where f "hello" = "wassup"
        f _ = "wat?"

But don't just special case textInteract for this use case. I want it to behave the same as interact in all situations.


Solution

  • http://hackage.haskell.org/packages/archive/text/0.11.2.0/doc/html/Data-Text-Lazy-IO.html

    has an interact already for you :)

    Seems like so does http://hackage.haskell.org/packages/archive/text/0.11.2.0/doc/html/Data-Text-IO.html