【问题标题】:Short circuiting a list of functions returns \/短路函数列表返回\/
【发布时间】:2015-03-19 10:15:38
【问题描述】:

所以我有一个使用这个签名的昂贵方法

def func(param: Int): \/[String, Int]

我正在尝试遍历参数列表并返回\/[String, List[Int]],但只要方法返回-\/,就停止循环。

我想出了这个:

scala> def func(i: Int) = {
     |   if( i > 1 ) { println{"!!"} ;"error".left[Int]}
     |   else i.right[String]
     | }
func: (i: Int)scalaz.\/[String,Int]

scala> val toList = (dis: scalaz.\/[String,Int]) => {dis.map(i => List(i))}
toList: scalaz.\/[String,Int] => scalaz.\/[String,List[Int]] = <function1>

scala> val composed = (func _) andThen toList
composed: Int => scalaz.\/[String,List[Int]] = <function1>

scala> (1 to 3).toList.foldLeftM(List[Int]().right[String]){(dis, i) =>
     |   for{
     |     a <- dis
     |     b <- composed(i)
     |   } yield a |+| b
     | }
<console>:17: error: no type parameters for method foldLeftM: (f: (scalaz.\/[String,List[Int]], Int) => G[scalaz.\/[String,List[Int]]])(implicit M: scalaz.Monad[G])G[scalaz.\/[String,List[Int]]] exist so that it can be applied to arguments ((scalaz.\/[String,List[Int]], Int) => scalaz.\/[String,List[Int]])
 --- because ---
argument expression's type is not compatible with formal parameter type;
 found   : (scalaz.\/[String,List[Int]], Int) => scalaz.\/[String,List[Int]]
 required: (scalaz.\/[String,List[Int]], Int) => ?G[scalaz.\/[String,List[Int]]]

              (1 to 2).toList.foldLeftM(List[Int]().right[String]){(dis, i) =>
                                       ^
<console>:17: error: type mismatch;
 found   : (scalaz.\/[String,List[Int]], Int) => scalaz.\/[String,List[Int]]
 required: (scalaz.\/[String,List[Int]], Int) => G[scalaz.\/[String,List[Int]]]
              (1 to 2).toList.foldLeftM(List[Int]().right[String]){(dis, i) =>
                                                                            ^

这里的G[_] 是什么,foldLeftM 中的正确结果类型是什么?

【问题讨论】:

    标签: scala scalaz either


    【解决方案1】:

    解决了我犯的两个错误:

    1. 误解了初始/结转值的类型,应该是List[Int]而不是\/
    2. 必须显式声明foldLeftM的类型参数

      scala> def func(i: Int) = {
       |   if( i > 1 ) { println{"!!"} ;"error".left[Int]}
       |   else i.right[String]
       | }
      func: (i: Int)scalaz.\/[String,Int]
      
      scala> val toList = (dis: scalaz.\/[String,Int]) => {dis.map(i => List(i))}
      toList: scalaz.\/[String,Int] => scalaz.\/[String,List[Int]] = <function1>
      
      scala> val composed = (func _) andThen toList
      composed: Int => scalaz.\/[String,List[Int]] = <function1>
      
      scala> val f = (dis: List[Int], i: Int) => composed(i).map(r => dis |+| r)
      f: (List[Int], Int) => scalaz.\/[String,List[Int]] = <function2>
      
      scala> (1 to 4).toList.foldLeftM[({type l[a] = String \/ a})#l, List[Int]](List[Int]())(f)
      !!
      res16: scalaz.\/[String,List[Int]] = -\/(error)
      

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-01-28
      • 1970-01-01
      • 2014-05-30
      • 2019-05-20
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多