Scala CAT中monad变压器的提升功能
假设抽象方法具有不同签名的特性(见下文)。为了便于理解,我可以为每个抽象方法定义相同的签名Scala CAT中monad变压器的提升功能,scala,functional-programming,monads,monad-transformers,scala-cats,Scala,Functional Programming,Monads,Monad Transformers,Scala Cats,假设抽象方法具有不同签名的特性(见下文)。为了便于理解,我可以为每个抽象方法定义相同的签名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: Sta
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
}
导入cats.data.{EitherT,Reader}
特征域{
键入Read[A]=读取器[BoundsProblem,A]
键入结果[A]=EitherT[Read,String,A]
def步长(s:State,direction:direction):结果[Double]//取决于注入的上下文,可能会失败
def takeStep(s:State,dir:Direction,stepSize:Double):Read[Variable]//取决于上下文,不能失败
def calculate(x:Variable):(Double,Gradient)//与上下文无关,不能失败
//不编译:
def迭代(s:状态,方向)=用于{
试一试
def迭代(s:State,dir:Direction):结果[(Double,Gradient)]=for{
谢谢。行teeeither[Read,Nothing,(Double,Gradient)]
。必需:Double=>EitherT[Read,String,(Double,Gradient)]
我猜您没有指定迭代(..)的返回类型:结果[(Double,Gradient)]
就像我做的那样。因此,要么指定返回类型,要么指定类型参数String
here:x
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)