title: How to use IO with Scalaz7 Iteratees without overflowing the stack?
body: <p>Consider this code (taken from <a href="https://stackoverflow.com/questions/13422756/handling-exceptions-in-an-iteratee-library-without-an-error-state">here</a> and modified to use bytes rather than lines of characters).</p>
  
  <pre><code>import java.io.{ File, InputStream, BufferedInputStream, FileInputStream }
  import scalaz._, Scalaz._, effect._, iteratee.{ Iteratee =&gt; I, _ }
  import std.list._
  
  object IterateeIOExample {
    type ErrorOr[+A] = EitherT[IO, Throwable, A]
  
    def openStream(f: File) = IO(new BufferedInputStream(new FileInputStream(f)))
    def readByte(s: InputStream) = IO(Some(s.read()).filter(_ != -1))
    def closeStream(s: InputStream) = IO(s.close())
  
    def tryIO[A, B](action: IO[B]) = I.iterateeT[A, ErrorOr, B] {
      EitherT(action.catchLeft).map(r =&gt; I.sdone(r, I.emptyInput))
    }
  
    def enumBuffered(r: =&gt; BufferedInputStream) = new EnumeratorT[Int, ErrorOr] {
      lazy val reader = r
      def apply[A] = (s: StepT[Int, ErrorOr, A]) =&gt; s.mapCont(k =&gt;
        tryIO(readByte(reader)) flatMap {
          case None =&gt; s.pointI
          case Some(byte) =&gt; k(I.elInput(byte)) &gt;&gt;== apply[A]
        })
    }
  
    def enumFile(f: File) = new EnumeratorT[Int, ErrorOr] {
      def apply[A] = (s: StepT[Int, ErrorOr, A]) =&gt;
        tryIO(openStream(f)).flatMap(stream =&gt; I.iterateeT[Int, ErrorOr, A](
          EitherT(
            enumBuffered(stream).apply(s).value.run.ensuring(closeStream(stream)))))
    }
  
    def main(args: Array[String]) {
      val action = (
        I.consume[Int, ErrorOr, List] &amp;=
        enumFile(new File(args(0)))).run.run
      println(action.unsafePerformIO())
    }
  }
  </code></pre>
  
  <p>Running this code on a decent-sized file (8kb) produces a StackOverflowException. Some searching turned up that the exception could be avoided by using the Trampoline monad instead of IO, but that doesn't seem like a great solution - sacrifice functional purity to get the program to complete at all. The obvious way to fix this is to use IO or Trampoline as a Monad Transformer to wrap the other, but I can't find an implementation of the transformer version of either of them and I'm not enough of a functional-programming guru to know how to write my own (learning more about FP is one of the purposes of this project, but I suspect creating new monad transformers is a bit above my level at the moment). I suppose I could just wrap a big IO action around creating, running and returning the result of my iteratees, but that feels like more of a workaround than a solution.</p>
  
  <p>Presumably some monads can't be converted to monad transformers, so I'd like to know if it's possible to work with large files without dropping IO or overflowing the stack, and if so, how?</p>
  
  <p>Bonus question: I can't think of any way for an iteratee to signal that it's encountered an error while processing except to have it return Either, which makes it less easy to compose them. The code above shows how to use EitherT to handle errors in the enumerator, but how does that work for the iteratees?</p>