title: How to create a play.api.libs.iteratee.Enumerator which inserts some data between the items of a given Enumerator?
body: <p>I use Play framework with ReactiveMongo. Most of ReactiveMongo APIs are based on the Play <code>Enumerator</code>. As long as I fetch some data from MongoDB and return it "as-is" asynchronously, everything is fine. Also the transformation of the data, like converting BSON to String, using <code>Enumerator.map</code> is obvious.</p>
  
  <p>But today I faced a problem which at the bottom line narrowed to the following code. I wasted half of the day trying to create an <code>Enumerator</code> which would consume items from the given <code>Enumerator</code> and insert some items between them. It is important not to load all the items at once, as there could be many of them (the code example has only two items "1" and "2"). But semantically it is similar to <code>mkString</code> of the collections. I am sure it can be done very easily, but the best I could come with - was this code. Very similar code creating an <code>Enumerator</code> using <code>Concurrent.broadcast</code> serves me well for WebSockets. But here even that does not work. The HTTP response never comes back. When I look at <code>Enumeratee</code>, it looks that it is supposed to provide such functionality, but I could not find the way to do the trick.</p>
  
  <p>P.S. Tried to call <code>chan.eofAndEnd</code> in <code>Iteratee.mapDone</code>, and <code>chunked(enums &gt;&gt;&gt; Enumerator.eof</code> instead of <code>chunked(enums)</code> - did not help. Sometimes the response comes back, but does not contain the correct data. What do I miss?</p>
  
  <pre><code>def trans(in:Enumerator[String]):Enumerator[String] = {
    val (res, chan) = Concurrent.broadcast[String]
  
    val iter = Iteratee.fold(true) { (isFirst, curr:String) =&gt;
      if (!isFirst)
        chan.push("&lt;--------&gt;")
      chan.push(curr)
      false
    }
  
    in.apply(iter)
  
    res
  }
  
  def enums:Enumerator[String] = {
    val en12 = Enumerator[String]("1", "2")
  
    trans(en12)
    //en12 //if I comment the previous line and uncomment this, it prints "12" as expected
  }
  
  def enum = Action {
    Ok.chunked(enums)
  }
  </code></pre>