gpt4 book ai didi

Scalaz sequenceU 和 IndexStateT 编译问题

转载 作者:行者123 更新时间:2023-12-01 13:31:04 26 4
gpt4 key购买 nike

我在下面的示例代码中有两个函数:runFrun

import scalaz._, Scalaz._
import scala.concurrent.Future

sealed class Controller[TState]
{
def genStartState (): TState = ???
implicit val m = Monoid.instance[Boolean] (_ | _, false)

def runF ()(implicit f: Monad[Future]) : Future[Boolean] =
{
val tasks : List[StateT[Future, TState, Boolean]]
= ???

val indexed : IndexedStateT[Future, TState, TState, List[Boolean]]
= tasks.sequenceU

val mapped : IndexedStateT[Future, TState, TState, Boolean]
= indexed.map { results => results.foldMap (identity) }

val result : Future[Boolean]
= mapped.eval (genStartState ())

result
}

def run[T[+_]: Monad] () : T[Boolean] =
{
val tasks: List[StateT[T, TState, Boolean]]
= ???

val indexed: IndexedStateT[T, TState, TState, List[Boolean]]
= tasks.sequenceU

val mapped: IndexedStateT[T, TState, TState, Boolean]
= indexed.map { results => results.foldMap (identity) }

val result: T[Boolean]
= mapped.eval (genStartState ())

result
}
}

runF 编译,run 不编译。

我不知道如何让 run 进行编译,我得到以下两个错误:

[error] could not find implicit value for parameter F: scalaz.MonadState[StateTT,CS]
[error] val monadStateT = MonadState[StateTT, CS]
[error] ^
[error] Implicit not found: scalaz.Unapply[scalaz.Applicative, scalaz.StateT[T,TState,Boolean]]. Unable to unapply type `scalaz.StateT[T,TState,Boolean]` into a type constructor of kind `M[_]` that is classified by the type class `scalaz.Applicative`. Check that the type class is defined by compiling `implicitly[scalaz.Applicative[type constructor]]` and review the implicits in object Unapply, which only cover common type 'shapes.'
[error] = stateTransformers.sequenceU
[error] ^

这两个函数之间的唯一区别是我试图使 runF 通用,因此我可以使用不同的 HKT。

最佳答案

不确定这是否是最好的方法,但我设法通过定义以下隐式 Applicative 使其工作:

type ST[α] = StateT[T, TState, α]

implicit val a: Applicative[ST] = new Applicative[ST] {

def point[A](a: => A): ST[A] =
StateT ((s: TState) => Applicative[T].point ((s, a)))

def ap[A, B](fa: => ST[A])(f: => ST[A => B]): ST[B] =
for { az <- fa; fz <- f } yield fz (az)
}

关于Scalaz sequenceU 和 IndexStateT 编译问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31537578/

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