Seeing that nobody gave a full answer, I'll convert my comment into one. One of the biggest advantages of conduit
is that it saves us from using lazy IO! Using a complicated WriterT
goes against the idea. Instead, we should make generator
a Source
and then just plug it with a file Sink
:
import Control.Monad
import Data.Conduit
import Data.Conduit.Binary
import qualified Data.ByteString.Char8 as BS
generator :: Monad m => Source m String
generator = replicateM_ 3 (yield "something\n")
-- or `forever (...)` if you want an infinite loop
-- Reads Strings, converts them to ByteStrings and writes
-- to a file.
sinkStream :: MonadResource m => FilePath -> Sink String m ()
sinkStream file = mapInput BS.pack (const Nothing) (sinkFile file)
main :: IO ()
main = runResourceT (generator $$ sinkStream "/tmp/output.txt")