Question

I was hoping to be able to create an Enumerator that would repeat a series of elements infinitely, but I'm having trouble figuring out a way. When I create a recursive Enumerator, I seem to overflow the stack when I try referencing it. For example, in order to create a Enumerator that repeats A,B,C,D,A,B,C,D,A,B,C,D,A... I was hoping to use something like:

scala> lazy val e1: Enumerator[String] = Enumerator("a","b","c","d") andThen e1
e1: play.api.libs.iteratee.Enumerator[String] = <lazy>

But then trying to use this in any way I get a stack overflow:

scala> e1 through Enumeratee.take(1) apply Iteratee.foreach(println(_)
java.lang.StackOverflowError
    at .e1$lzycompute(<console>:11)
    at .e1(<console>:11)
    at .e1$lzycompute(<console>:11)
    at .e1(<console>:11)
    at .e1$lzycompute(<console>:11)
    at .e1(<console>:11)
Was it helpful?

Solution

AHA! answering my own question

Thanks to both @travis and @cmbaxter to pointing me into good directions. I was looking at both old source code and old api docs and didn't see newer methods like repeat. The ticket for me is unfold:

scala> val s: Stream[String] = "A" #:: "B" #:: "C" #:: "D" #:: s
s: Stream[String] = Stream(A, ?)

scala> Enumerator.unfold(s)(s => Some(s.tail, s.head)) through Enumeratee.take(10) apply Iteratee.foreach(println(_))
res3: scala.concurrent.Future[play.api.libs.iteratee.Iteratee[String,Unit]] = scala.concurrent.impl.Promise$DefaultPromise@55d3e052

scala> A
B
C
D
A
B
C
D
A
B
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top