2013-06-03 170 views
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] = ??? 

有沒有人遇到過類似的enumeratee實施?

+1

你的意思是這樣的星形或scalaz流兩個函數(https://github.com/scalaz/scalaz-stream/blob/master/的src /主/階/ scalaz /流/ Process.scala)? –

+0

是的。但是看起來Enumeratee是不行的,相應的,如果你在組合的Enumeratees中保持任何處理狀態,你就無法做到這一點。 –

回答

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 } 
    } 
}