title: How to avoid stair-stepping with Monad Transformers in scala?
body: <p>I have the following code that uses the <code>Reader</code> monad for configuration and also has to deal with <code>IO[Option[String]]</code> and I've ended up with code that stair-steps in my <code>encode</code> function.</p>
  
  <p>How can I formulate a monad transformer for <code>Reader</code> and <code>OptionT</code> to avoid the ugly nested <code>for</code> comprehensions in my <code>encode</code> function?</p>
  
  <pre><code>def encode(fileName: String): Reader[Config, IO[Unit]] = for {
     ffmpegWrapper &lt;- findFfmpegWrapper
     ffmpegBin &lt;- findFfmpeg
  } yield (for {
      w &lt;- ffmpegWrapper
      b &lt;- ffmpegBin
      stream &lt;- callFfmpeg(getCommand(w, b, fileName)).liftM[OptionT]
  } yield stream) map (_ foreach (println)) getOrElse Unit.box {}
  
  
  def getCommand(ffmpegWrapper: String, ffmpegBin: String,
               videoFile: String) = s"$ffmpegWrapper $ffmpegBin $videoFile  '-vcodec libx264 -s 1024x576' /tmp/out.mp4"
  
  def callFfmpeg(command: String): IO[Stream[String]] = IO {
    Process(command).lines_!
  }
  
  def findFile(path:List[String]): OptionT[IO,String] = OptionT[IO,String](IO{path.find(new File(_).exists)})
  
  def findFfmpeg:Reader[Config, OptionT[IO,String]] = Reader {c=&gt;findFile(c.ffmpegLocations)}
  
  def findFfmpegWrapper:Reader[Config, OptionT[IO,String]] = Reader {c=&gt;findFile(c.ffmpegWrapperLocations)}
  </code></pre>
  
  <p>Thank-you!</p>