`interact` using Text instead of String

307 views Asked by At

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.

1

There are 1 answers