问题描述
我想将一个枚举数应用于iteratee,然后再取回原来的iteratee,因此我可以再应用一些东西.播放文档中有一个示例,该示例使用Iteratee [Int,Int]来汇总其输入(http://www.playframework.org/documentation/2.0.1/Enumeratees).然后,他们使用Enumeratee [String,Int]允许输入类似"3"和"6"的字符串.示例如下:
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
但这不会编译,因为Enumerator.>>>将另一个Enumerator作为参数-不是iteratee.我尝试使用| >>代替:
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
但是此示例不打印"28",而是打印"10".似乎只考虑添加到已修改iteratee的部分.
But this example doesn't print "28" but "10". It seems to only consider the parts added to the adapted iteratee.
使用枚举对象时如何找回原始iteratee?
How can I get the original iteratee back when using an enumeratee?
推荐答案
如果您使用的是2.0版本,则此错误已在更高版本中修复.枚举对象过去一直发送它收到的EOF,这就是bug.
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.
这篇关于Scala:应用枚举后获取原始iteratee(播放文档中的示例未编译)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!