有条件地组合枚举

时间:2013-06-03 09:55:05

标签: scala playframework-2.0

是否有人遇到过这段代码来实现枚举的条件组合?基本上我给了

val decideEnumeratee : Enumerate[A,Either[L,R] = Enumerate.map(a=>???)
val leftSideEnumeratee : Enumeratee[L,B] = Enumeratee.map(l=>???)
val rightEnumeratee: Enumeratee[R,B] = Enumeratee.map(r=>???)

我希望实现以下组合器:

def either[L,R,B](left:Enumeratee[L,B], right,Enumeratee[R,B]): Enumeratee[Either[L,R],B] = ???

有没有人遇到类似的枚举实现?

1 个答案:

答案 0 :(得分:0)

以下是either的定义:

def either[A, B, C](left: Enumeratee[A, C], right: Enumeratee[B, C])
  (implicit ec: ExecutionContext) = new Enumeratee[Either[A, B], C] {
    def applyOn[IR](inner: Iteratee[C, IR]) = {
      val (liter, lenum) = Concurrent.joined[C]
      val (riter, renum) = Concurrent.joined[C]
      val liter2 = Enumeratee.mapConcat { x: Either[A, B] =>
          x.left.toSeq
        } compose left transform liter
      val riter2 = Enumeratee.mapConcat { x: Either[A, B] =>
          x.right.toSeq
        } compose right transform riter
      val fresult = lenum interleave renum apply inner
      Enumeratee.zip(liter2, riter2) mapM { _ => fresult }
    }
}