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

106 views
Skip to first unread message

Henry Story

unread,
Mar 21, 2013, 5:14:22 AM3/21/13
to play-fr...@googlegroups.com
Hi,

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 [1] could be used to do this - though it looks pretty inefficient.

Henry

PS. I put this up on StackOverflow
http://stackoverflow.com/questions/15543261/transforming-a-seqfuturex-into-an-enumeratorx

[1] https://gist.github.com/viktorklang/4488970



Social Web Architect
http://bblfish.net/

Nabloom

unread,
Mar 22, 2013, 10:37:45 AM3/22/13
to play-fr...@googlegroups.com
Hi,

don't know if it's the most efficient way but you can :
  1. turn Seq[Future[X]] to Seq[Enumerator[X]] using Enumerator.generateM
  2. turn Seq[Enumerator[X]] to Enumerator[X] using Enumerator.interleave
Nab

Henry Story

unread,
Mar 25, 2013, 12:03:12 PM3/25/13
to play-fr...@googlegroups.com


On Friday, 22 March 2013 15:37:45 UTC+1, Nabloom wrote:
Hi,

don't know if it's the most efficient way but you can :
  1. turn Seq[Future[X]] to Seq[Enumerator[X]] using Enumerator.generateM
  2. turn Seq[Enumerator[X]] to Enumerator[X] using Enumerator.interleave

Thanks. I got the following to work:

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.foldRight(Future.successful(i)) {
              case (x, i) => i.flatMap(_.feed(Input.El(x)))
            }
        }
      }
    }

and posted it on StackOverflow...
Reply all
Reply to author
Forward
0 new messages