提升斯卡拉猫的单体变压器的功能



假设抽象方法具有不同签名的特征(见下文(。为了便于理解,我可以为每个抽象方法定义相同的签名Result[A]

但是,为了简化 trait 的子类,我想保留方法 2 和 3 的更简单的签名。

import cats.data.{EitherT, Reader}
trait Domain{
type  Read[A] = Reader[BoundsProblem, A]
type Result[A] = EitherT[Read, String, A]
def stepSize( s: State, direction: Direction): Result[Double] //depends on an injected context, can fail
def takeStep( s: State, dir: Direction, stepSize: Double): Read[Variable] //depends on context, can't fail
def calculate(x: Variable): (Double, Gradient) //context-independent, can't fail
//doesn't compile: 
def iteration(s: State, dir: Direction) =  for{
tee <- stepSize(s, dir)
x <- takeStep(s, dir, tee)
r <- calculate(x)
} yield  r
}

我的问题是如何在猫中做到这一点。(我试图将takeStep提升到EitherT[Read, String, A]但没有成功。还是我最好为每个方法定义相同的Result[A]

尝试

def iteration(s: State, dir: Direction): Result[(Double, Gradient)] =  for{
tee <- stepSize(s, dir)
x   <- EitherT.right(takeStep(s, dir, tee))
r   = calculate(x)
} yield r

def iteration(s: State, dir: Direction): Result[(Double, Gradient)] =  for{
tee <- stepSize(s, dir)
x   <- EitherT.right(takeStep(s, dir, tee))
} yield calculate(x)