Question

I want to apply an enumeratee to an iteratee and afterwards get the original iteratee back, so I can apply further stuff. There is an example in the play documentation which uses an Iteratee[Int,Int] that just sums up its input (http://www.playframework.org/documentation/2.0.1/Enumeratees). Then they use an Enumeratee[String,Int] that allows strings like "3" and "6" as input. The example is as follows:

val sum:Iteratee[Int,Int] = Iteratee.fold[Int,Int](0){ (s,e) => s + e }

//create am Enumeratee using the map method on Enumeratee
val toInt: Enumeratee[String,Int] = Enumeratee.map[String]{ s => s.toInt } 

val adaptedIteratee: Iteratee[String,Iteratee[Int,Int]] = toInt(sum)

// pushing some strings
val afterPushingStrings: Iteratee[String,Iteratee[Int,Int]] = {
  Enumerator("1","2","3","4") >>> adaptedIteratee
}

val originalIteratee: Iteratee[Int,Int] = flatten(afterPushingString.run)

val moreInts: Iteratee[Int,Int] = Enumerator(5,6,7) >>> originalIteratee

moreInts.run.onRedeem(sum => println(sum) ) // eventually prints 28

But this doesn't compile, because Enumerator.>>> takes another Enumerator as parameter - not an iteratee. I tried it using |>> instead:

val sum: Iteratee[Int, Int] = Iteratee.fold[Int, Int](0) { (s, e) => s + e }

//create am Enumeratee using the map method on Enumeratee
val toInt: Enumeratee[String, Int] = Enumeratee.map[String] { s => s.toInt }

val adaptedIteratee: Iteratee[String, Iteratee[Int, Int]] = toInt(sum)

// pushing some strings
val afterPushingStrings: Iteratee[String, Iteratee[Int, Int]] = {
  Iteratee.flatten(Enumerator("1", "2", "3", "4") |>> adaptedIteratee)
}

val originalIteratee: Iteratee[Int, Int] = Iteratee.flatten(afterPushingStrings.run)

val moreInts: Iteratee[Int, Int] = Iteratee.flatten(Enumerator(5, 6, 7) |>> originalIteratee)

moreInts.run.onRedeem(sum => println("Sum="+sum)) // eventually prints 28

But this example doesn't print "28" but "10". It seems to only consider the parts added to the adapted iteratee.

How can I get the original iteratee back when using an enumeratee?

Was it helpful?

Solution

If you are using 2.0 release then this is a bug that was fixed in later releases. Enumeratee used to send along the EOF it receives, and that was the bug.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top