1

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.

Dan Burton
  • 53,238
  • 27
  • 117
  • 198

1 Answers1

5

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

singpolyma
  • 10,999
  • 5
  • 47
  • 71
  • 1
    *doh*. I should have looked more carefully at the docs. :) By replacing `textInteract` with `T.interact` and adding `import qualified Data.Text.Lazy.IO as T`, the latter version of the code does indeed behave just like its `String` counterpart. It remains to be demonstrated that it *always* behaves like its `String` counterpart, but for now I am satisfied. – Dan Burton Aug 13 '12 at 21:41