Я хочу применить перечисление к итерируемому, а затем вернуть исходное итерируемое, чтобы я мог применить дополнительные материалы. В документации по игре есть пример, в котором используется 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, а не итерируемый. Вместо этого я попробовал использовать |>>:
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». Кажется, что учитываются только те части, которые были добавлены к адаптированной итерации.
Как я могу вернуть исходный итерируемый объект при использовании перечисляемого?