【问题标题】:Scala dotless call syntax with curried function带有柯里化函数的 Scala 无点调用语法
【发布时间】:2010-10-27 11:12:55
【问题描述】:

注意:更一般的问题的详细答案在 Stack Overflow 问题中What are the precise rules for when you can omit parenthesis, dots, braces, = (functions), etc.?

以下作品:

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)(_ + _)
                                        ^

建议的修复方法是什么?

【问题讨论】:

    标签: scala syntax


    【解决方案1】:

    Stack Overflow 问题 What are the precise rules for when you can omit parenthesis, dots, braces, = (functions), etc.? 中很好地描述了这个主题。

    Curried 函数似乎比只有一个参数的方法难一些。要省略点,柯里化函数需要在中缀调用之外使用括号。

    作为Marimuthu Madasamy mentioned,这可行(对象(List)、方法(foldLeft)及其第一个参数(0)在括号中):

    (List(1,2,3) filter (_ > 1) foldLeft 0) (_ + _)
    

    【讨论】:

    • Marimuthu,你应该加入你的答案。
    【解决方案2】:

    这行得通:

    (List(1,2,3) filter (_ > 1) foldLeft 0) (_ + _)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-02-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-03-14
      • 2012-12-27
      • 1970-01-01
      相关资源
      最近更新 更多