Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/scala/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Scala 如何为状态、析取和列表的单子堆栈定义函数?_Scala_Scalaz_Monad Transformers - Fatal编程技术网

Scala 如何为状态、析取和列表的单子堆栈定义函数?

Scala 如何为状态、析取和列表的单子堆栈定义函数?,scala,scalaz,monad-transformers,Scala,Scalaz,Monad Transformers,我有一个列表monadlist[Int]。我希望根据列表的值生成效果(析取和状态)。这是我的monad堆栈和运行monad堆栈的代码。我的问题是,定义checkNum的正确方法应该是什么,以便生成正确的效果 我的预期输出应 列表((“”,\/-(1),(“错误:2”,-\/(Throwable()),(“”,\/-(3)),(“错误:4”,-\/(Throwable())) 导入scalaz_ 进口Scalaz_ 输入EitherTH[F[]A]=EitherT[F,Throwable,A] 类

我有一个列表monad
list[Int]
。我希望根据列表的值生成效果(析取和状态)。这是我的monad堆栈和运行monad堆栈的代码。我的问题是,定义checkNum的正确方法应该是什么,以便生成正确的效果

我的预期输出应
列表((“”,\/-(1),(“错误:2”,-\/(Throwable()),(“”,\/-(3)),(“错误:4”,-\/(Throwable()))

导入scalaz_
进口Scalaz_
输入EitherTH[F[]A]=EitherT[F,Throwable,A]
类型StateTH[F[\u],A]=StateT[F,String,A]
类型StateTList[A]=StateTH[List,A]
键入EitherTStateTList[A]=EitherTH[StateTList,A]
val lst=列表(1,2,3,4)
def checkNum(x:Int)(隐式ms:MonadState[eitherstatetlist,Int])=if((x%2)==0){
放置(s“错误:$x”)
-\/(新的可丢弃()
}否则{
放入(“”)
\/-(十)
}  
val prg=用于{

在我看来,
checkNum
应该返回一个
状态[String,\/[Throwable,Int]]

def checkNum0(x:Int):状态[String,\/[Throwable,Int]]=if((x%2)==0){
constantState(\/(new Throwable()),s“Error:$x”)
}否则{
恒常状态(\/-(x),“”)
}
def checkNum1(x:Int):状态列表[\/[Throwable,Int]]=checkNum0(x).提升[List]
然后,您可以将您的理解写为:

val prg=for{

在我看来,
checkNum
应该返回一个
状态[String,\/[Throwable,Int]]

def checkNum0(x:Int):状态[String,\/[Throwable,Int]]=if((x%2)==0){
constantState(\/(new Throwable()),s“Error:$x”)
}否则{
恒常状态(\/-(x),“”)
}
def checkNum1(x:Int):状态列表[\/[Throwable,Int]]=checkNum0(x).提升[List]
然后,您可以将您的理解写为:

val prg=for{

x在scala、try-eff或emm中使用>2个单体是不方便的。在scala、try-eff或emm中使用>2个单体是不方便的。
import scalaz._
import Scalaz._

type EitherTH[F[_], A] = EitherT[F, Throwable,A]
type StateTH[F[_], A] = StateT[F, String, A]
type StateTList[A] = StateTH[List, A]
type EitherTStateTList[A] = EitherTH[StateTList, A]
val lst = List(1,2,3,4)

def checkNum(x:Int)(implicit ms:MonadState[EitherTStateTList, Int]) = if ((x%2)==0) {
  put(s"Error: $x")
  -\/(new Throwable())
}  else {
   put("")
   \/-(x)
}  

val prg = for {
  x <- lst.liftM[StateTH].liftM[EitherTH]
  // y <- checkNum(x).liftM[EitherTH]
} yield y 

prg.run("")