我想将 enumeratee 应用于 iteratee,然后取回原始的 iteratee,这样我就可以应用更多的东西。播放文档中有一个示例,它使用 Iteratee[Int,Int] 来总结其输入(http://www.playframework.org/documentation/2.0.1/Enumeratees)。然后他们使用 Enumeratee[String,Int] 允许像“3”和“6”这样的字符串作为输入。示例如下:
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。我尝试使用 |>> 代替:
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”。它似乎只考虑添加到适应迭代的部分。
使用枚举对象时如何取回原始迭代对象?