title: How do I flatten a nested For Comprehension that uses I/O?
body: <p>I am having trouble flattening a nested For Generator into a single For Generator.</p>
  
  <p>I created <strong>MapSerializer</strong> to save and load Maps.</p>
  
  <p>Listing of <strong>MapSerializer.scala</strong>:</p>
  
  <pre><code>import java.io.{ObjectInputStream, ObjectOutputStream}
  
  object MapSerializer {
    def loadMap(in: ObjectInputStream): Map[String, IndexedSeq[Int]] =
      (for (_ &lt;- 1 to in.readInt()) yield {
        val key = in.readUTF()
        for (_ &lt;- 1 to in.readInt()) yield {
          val value = in.readInt()
          (key, value)
        }
      }).flatten.groupBy(_ _1).mapValues(_ map(_ _2))
  
    def saveMap(out: ObjectOutputStream, map: Map[String, Seq[Int]]) {
      out.writeInt(map size)
      for ((key, values) &lt;- map) {
        out.writeUTF(key)
        out.writeInt(values size)
        values.foreach(out.writeInt(_))
      }
    }
  }
  </code></pre>
  
  <p>Modifying <strong>loadMap</strong> to assign <strong>key</strong> within the generator causes it to fail:</p>
  
  <pre><code>def loadMap(in: ObjectInputStream): Map[String, IndexedSeq[Int]] =
    (for (_ &lt;- 1 to in.readInt();
          key = in.readUTF()) yield {
      for (_ &lt;- 1 to in.readInt()) yield {
        val value = in.readInt()
        (key, value)
      }
    }).flatten.groupBy(_ _1).mapValues(_ map(_ _2))
  </code></pre>
  
  <p>Here is the stacktrace I get:</p>
  
  <pre><code>java.io.UTFDataFormatException
      at java.io.ObjectInputStream$BlockDataInputStream.readWholeUTFSpan(ObjectInputStream.java)
      at java.io.ObjectInputStream$BlockDataInputStream.readOpUTFSpan(ObjectInputStream.java)
      at java.io.ObjectInputStream$BlockDataInputStream.readWholeUTFSpan(ObjectInputStream.java)
      at java.io.ObjectInputStream$BlockDataInputStream.readUTFBody(ObjectInputStream.java)
      at java.io.ObjectInputStream$BlockDataInputStream.readUTF(ObjectInputStream.java:2819)
      at java.io.ObjectInputStream.readUTF(ObjectInputStream.java:1050)
      at MapSerializer$$anonfun$loadMap$1.apply(MapSerializer.scala:8)
      at MapSerializer$$anonfun$loadMap$1.apply(MapSerializer.scala:7)
      at scala.collection.TraversableLike$$anonfun$map$1.apply(TraversableLike.scala:194)
      at scala.collection.TraversableLike$$anonfun$map$1.apply(TraversableLike.scala:194)
      at scala.collection.immutable.Range.foreach(Range.scala:76)
      at scala.collection.TraversableLike$class.map(TraversableLike.scala:194)
      at scala.collection.immutable.Range.map(Range.scala:43)
      at MapSerializer$.loadMap(MapSerializer.scala:7)
  </code></pre>
  
  <p>I would like to flatten the loading code to a single For Comprehension, but I get errors that suggest that it is either executing in a different order or repeating steps I am not expecting it to repeat.</p>
  
  <p>Why is it that moving the assignment of <strong>key</strong> into the generator causes it to fail?</p>
  
  <p>Can I flatten this into a single generator?  If so, what would that generator be?</p>