具有咖喱功能的Scala无点呼叫语法
问题描述:
注意:对于更一般问题的详细答案在Stack Overflow问题What are the precise rules for when you can omit parenthesis, dots, braces, = (functions), etc.?。具有咖喱功能的Scala无点呼叫语法
以下工作:
scala> List(1,2,3) filter (_ > 1) reduceLeft(_ + _)
res65: Int = 5
而且还有如下:
scala> List(1,2,3).filter(_ > 1).foldLeft(0)(_ + _)
res67: Int = 5
而不是现在这样的语法时才:
scala> List(1,2,3) filter (_ > 1) foldLeft(0)(_ + _)
<console>:10: error: 0 of type Int(0) does not take parameters
List(1,2,3) filter (_ > 1) foldLeft(0)(_ + _)
^
什么是修复建议?
答
本主题中的堆栈溢出问题What are the precise rules for when you can omit parenthesis, dots, braces, = (functions), etc.?有很好的描述。
Curried功能似乎比具有一个参数的方法稍难一点。为了省略点,curried函数需要在中缀调用之外使用括号。
由于Marimuthu Madasamy mentioned,这个工作(对象(表),方法(foldLeft)和它的第一个参数(0)是在括号中):
(List(1,2,3) filter (_ > 1) foldLeft 0) (_ + _)
答
这工作:
(List(1,2,3) filter (_ > 1) foldLeft 0) (_ + _)
Marimuthu,你应该加入你的答案。 – mkneissl 2010-10-27 12:50:44