scala – 使用反向关联中缀表示法部分应用curried函数的语法
发布时间:2020-12-16 18:37:45 所属栏目:安全 来源:网络整理
导读:换句话说,有没有一个很好的理由为什么不应该编译? def f(xs: List[Int]) = xs.foldLeft(0) _ // OKdef f(xs: List[Int]) = (xs : 0) _ // OKdef f(xs: List[Int]) = (0 /: xs) _console:15: error: missing arguments for method /: in trait TraversableO
换句话说,有没有一个很好的理由为什么不应该编译?
def f(xs: List[Int]) = xs.foldLeft(0) _ // OK def f(xs: List[Int]) = (xs : 0) _ // OK def f(xs: List[Int]) = (0 /: xs) _ <console>:15: error: missing arguments for method /: in trait TraversableOnce; follow this method with `_' if you want to treat it as a partially applied function 以下是一些解决方法: def f(xs: List[Int]) = xs./:(0) _ def f(xs: List[Int]): ((Int,Int) => Int) => Int = (0 /: xs) 但我的问题主要是关于一般的正确语法. 解决方法
我刚刚解决了这个问题,但是我还不能检查它,因为它需要修改规范.
scala> def f(xs: List[Int]) = (0 /: xs) _ f: (xs: List[Int])(Int,Int) => Int => Int scala> f(1 to 10 toList) res0: (Int,Int) => Int => Int = <function1> scala> res0(_ + _) res1: Int = 55 问题是,如果op是右关联的,则规范定义“e1 op e2”为{val x = e1; e2.op(x)}由于我不明白的原因,因为更简单的e2.op(e1)解决了这个问题,如https://issues.scala-lang.org/browse/SI-1980.我将进行查询. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |