6

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

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] = ???

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

4

1 回答 1

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 }
    }
}
于 2014-05-11T00:29:02.520 回答