Last active
December 10, 2015 22:29
-
-
Save dohzya/4502622 to your computer and use it in GitHub Desktop.
Test de séparation d'un Enumerator (de Play 2) en 2 parties.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package tests | |
import play.api._ | |
import play.api.libs.concurrent._ | |
import play.api.libs.iteratee._ | |
import scala.concurrent._ | |
import scala.concurrent.ExecutionContext.global | |
object TestEnum { | |
def basic { | |
val enum = Enumerator.enumerate(1 to 100) | |
val eitherEnum = enum &> Enumeratee.map { | |
case i if i % 2 == 0 => Right(i) | |
case i => Left(i) | |
} | |
val ko = eitherEnum &> Enumeratee.collect { case Left(x) => x } | |
val ok = eitherEnum &> Enumeratee.collect { case Right(x) => x } | |
ok |>> Iteratee.foreach(x => println(s"ok: $x")) | |
ko |>> Iteratee.foreach(x => println(s"ko: $x")) | |
} | |
def broadcast { | |
val enum = Enumerator.enumerate(1 to 100) | |
val eitherEnum = enum &> Enumeratee.map { | |
case i if i % 2 == 0 => Right(i) | |
case i => Left(i) | |
} | |
val (broadcast, _) = Concurrent.broadcast(eitherEnum) | |
val ko = broadcast &> Enumeratee.collect { case Left(x) => x } | |
val ok = broadcast &> Enumeratee.collect { case Right(x) => x } | |
ok |>> Iteratee.foreach(x => println(s"ok: $x")) | |
ko |>> Iteratee.foreach(x => println(s"ko: $x")) | |
} | |
} |
Nouvelle version utilisant broadcast
: les nombres sont maintenant affichés dans l'ordre… mais commencent à partir de 2. Est-ce que ça veut dire que broadcast consomme l'enumerator même si personne ne lit ?
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Chez moi ça affiche bien les nombres de 1 à 100 (dans le désordre). Mais est-ce normal ou juste un cas particulier ?