transforming a Seq[Future[X]] into an Enumerator[X]

playframework-2.0, scala

Solution

A better, shorter and I think more efficient answer is: `

   def toEnumerator(seqFutureX: Seq[Future[X]]) = new Enumerator[X] { 
      def apply[A](i: Iteratee[X, A]): Future[Iteratee[X, A]] = {
        Future.sequence(seqFutureX).flatMap { seqX: Seq[X] => 
            seqX.foldLeft(Future.successful(i)) {
              case (i, x) => i.flatMap(_.feed(Input.El(x)))
            }
        }
      }
    }

`

Problem

Is there a way to turn a Seq[Future[X]] into an Enumerator[X] ? The use case is that I want to get resources by crawling the web. This is going to return a Sequence of Futures, and I'd like to return an Enumerator that will push the futures in the order in which they are first finished on to the Iteratee. It looks like Victor Klang's Future select gist could be used to do this - though it looks pretty inefficient. Note: The Iteratees and Enumerator's in question are those given by the play framework version 2.x, ie with the following imports: `import play.api.libs.iteratee._`

Original source