Skip to content

Instantly share code, notes, and snippets.

@dohzya
Last active December 10, 2015 22:29
Show Gist options
  • Save dohzya/4502622 to your computer and use it in GitHub Desktop.
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.
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"))
}
}
@dohzya
Copy link
Author

dohzya commented Jan 10, 2013

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