gpt4 book ai didi

scala - 有条件地组合枚举

转载 作者:行者123 更新时间:2023-12-04 16:11:00 25 4
gpt4 key购买 nike

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

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 实现?

最佳答案

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

关于scala - 有条件地组合枚举,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16894096/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com