`
bit1129
  • 浏览: 1051457 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

【Scala十七】Scala核心十一:下划线_的用法

 
阅读更多

下划线_在Scala中广泛应用,_的基本含义是作为占位符使用。_在使用时是出问题非常多的地方,本文将不断完善_的使用场景以及所表达的含义

 

1. 在高阶函数中使用

scala> val list = List(-3,8,7,9)
list: List[Int] = List(-3, 8, 7, 9)

scala> list.filter(_ > 7)
res8: List[Int] = List(8, 9)

 

_ > 7是一个函数字面量,_表示调用这个函数时提供的参数值,按理说,应该指定类型,但是这里没有指定,原因是可以通过filter操作推导出来

 

如下写法不对:

scala> list.filter(_:Int=>_>7)
<console>:1: error: identifier expected but integer literal found.
       list.filter(_:Int=>_>7)
                            ^

 

 

2. 省略函数列表

 

scala> val f = (_:Int) + (_:Int)
f: (Int, Int) => Int = <function2>

scala> f(2,3)
res11: Int = 5

scala> val f=(x:Int,y:Int)=>x+y
f: (Int, Int) => Int = <function2>

scala> f(2,3)
res12: Int = 5

 

 通过val f = (_:Int) + (_:Int),我们只定义了函数f的方法体,并没有指定函数的参数列表,_和_分表表示第一个和第二个参数,

_ + _ expands into a literal for a function that takes two parameters.

限制: You can use this short form only if each parameter appears in the function literal at most once. (我认为是exactly once)Multiple underscores mean multiple parameters,not reuse of a single parameter repeatedly. The first underscore represents the first parameter, the second underscore the second parameter, the third underscore the third parameter, and so on.

 

 

 

如下定义错误:

scala> val f = _ + _
<console>:7: error: missing parameter type for expanded function ((x$1, x$2) => x$1.$plus(x$2))
       val f = _ + _
               ^
<console>:7: error: missing parameter type for expanded function ((x$1: <error>, x$2) => x$1.$plus(x$2))
       val f = _ + _
                   ^

 

但是如下操作正确

scala> val list = List(-3,8,7,9)
list: List[Int] = List(-3, 8, 7, 9)

scala> list.reduce(_ + _)
res13: Int = 21

 

3. 部分应用函数

scala> val f = (a:Int,b:Int,c:Int)=>a+b+c
f: (Int, Int, Int) => Int = <function3>

scala> val sum = f _
sum: () => (Int, Int, Int) => Int = <function0>

 

 如下写法错误:

 

scala> val sum = f(1) _
<console>:9: error: not enough arguments for method apply: (v1: Int, v2: Int, v3: Int)Int in trait Function3.
Unspecified value parameters v2, v3.
       val sum = f(1) _
                  ^

 

如下写法正确

scala> val sum = f(1,_:Int,_:Int)
sum: (Int, Int) => Int = <function2>

 

如下写法错误:

scala> val sum = f(1,_,_)
<console>:9: error: missing parameter type for expanded function ((x$1, x$2) => f(1, x$1, x$2))
       val sum = f(1,_,_)
                     ^
<console>:9: error: missing parameter type for expanded function ((x$1: <error>, x$2) => f(1, x$1, x$2))
       val sum = f(1,_,_)
                       ^

 原因是,f函数可能会有重载,假如f有两个具有三个参数的重载函数,那么调用f(1,_,_),编译器将不知道调用哪个函数将第一个参数(这里是1)代入,而f _则没有参数代入的步骤,这个在后期进行参数应用时,才会找具体的f

 关于这个问题,http://stackoverflow.com/questions/8549393/redundant-parameter-type-info-in-partially-applied-function-definition有讨论

 

分享到:
评论

相关推荐

    Apache Kafka 3.2.0 (Scala 2.12 :kafka_2.12-3.2.0.tgz)

    Apache Kafka 3.2.0 (Scala 2.12 :kafka_2.12-3.2.0.tgz) 是一个开源分布式事件流平台,被数千家公司用于高性能数据管道、流分析、数据集成和关键任务应用程序。) 是一个开源分布式事件流平台,被数千家公司用于高...

    scala-xml_2.12-1.0.6-API文档-中文版.zip

    赠送jar包:scala-xml_2.12-1.0.6.jar;...使用方法:解压翻译后的API文档,用浏览器打开“index.html”文件,即可纵览文档内容。 人性化翻译,文档中的代码和结构保持不变,注释和说明精准翻译,请放心使用。

    scala-xml_2.11-1.0.4-API文档-中英对照版.zip

    使用方法:解压翻译后的API文档,用浏览器打开“index.html”文件,即可纵览文档内容。 人性化翻译,文档中的代码和结构保持不变,注释和说明精准翻译,请放心使用。 双语对照,边学技术、边学英语。

    scala-java8-compat_2.11-0.7.0-API文档-中文版.zip

    赠送jar包:scala-java8-compat_2.11-0.7.0.jar;...使用方法:解压翻译后的API文档,用浏览器打开“index.html”文件,即可纵览文档内容。 人性化翻译,文档中的代码和结构保持不变,注释和说明精准翻译,请放心使用。

    scala::nerd_face:高级示例:face_with_monocle:项目Akka:rocket:ZIO:high_voltage:算法:cat_with_wry_smile:猫

    :warning: 请不要编辑该文件。 您应该使用命令sbt docs/mdoc编辑modules/docs/src/main/mdoc/README.md sbt docs/mdoc ... Scala是一个具有许多概念证明模块的项目,使用了围绕Scala编程语言的所有内容

    scala-parser-combinators_2.11-1.0.4-API文档-中英对照版.zip

    使用方法:解压翻译后的API文档,用浏览器打开“index.html”文件,即可纵览文档内容。 人性化翻译,文档中的代码和结构保持不变,注释和说明精准翻译,请放心使用。 双语对照,边学技术、边学英语。

    scala-xml_2.11-1.0.5-API文档-中英对照版.zip

    使用方法:解压翻译后的API文档,用浏览器打开“index.html”文件,即可纵览文档内容。 人性化翻译,文档中的代码和结构保持不变,注释和说明精准翻译,请放心使用。 双语对照,边学技术、边学英语。

    scala-parser-combinators-2.11-1.0.4-API文档-中文版.zip

    赠送jar包:scala-parser-combinators_2.11-1.0.4.jar;...使用方法:解压翻译后的API文档,用浏览器打开“index.html”文件,即可纵览文档内容。 人性化翻译,文档中的代码和结构保持不变,注释和说明精

    flink-scala_2.12-1.14.3-API文档-中文版.zip

    赠送jar包:flink-scala_2.12-1.14.3.jar ...使用方法:解压翻译后的API文档,用浏览器打开“index.html”文件,即可纵览文档内容。 人性化翻译,文档中的代码和结构保持不变,注释和说明精准翻译,请放心使用。

    php-scala-migration-example::light_bulb:一个示例项目,用于描述将 UI 逐步从 PHP 迁移到 Scala

    一个示例项目,用于描述逐步从 PHP 迁移 UI 到 Scala old-ui : 内置 PHP。 new-ui : 内置于 Scala。

    scala-xml_2.11-1.0.1-API文档-中文版.zip

    赠送jar包:scala-xml_2.11-1.0.1.jar;...使用方法:解压翻译后的API文档,用浏览器打开“index.html”文件,即可纵览文档内容。 人性化翻译,文档中的代码和结构保持不变,注释和说明精准翻译,请放心使用。

    scala-java8-compat_2.11-0.7.0-API文档-中英对照版.zip

    使用方法:解压翻译后的API文档,用浏览器打开“index.html”文件,即可纵览文档内容。 人性化翻译,文档中的代码和结构保持不变,注释和说明精准翻译,请放心使用。 双语对照,边学技术、边学英语。

    Manning_Scala_in_Depth_May_2012.pdf

    Manning_Scala_in_Depth_May_2012.pdf Manning_Scala_in_Depth_May_2012.pdf Manning_Scala_in_Depth_May_2012.pdf

    scala-xml_2.11-1.0.5-API文档-中文版.zip

    赠送jar包:scala-xml_2.11-1.0.5.jar;...使用方法:解压翻译后的API文档,用浏览器打开“index.html”文件,即可纵览文档内容。 人性化翻译,文档中的代码和结构保持不变,注释和说明精准翻译,请放心使用。

    scala-xml_2.11-1.0.1-API文档-中英对照版.zip

    使用方法:解压翻译后的API文档,用浏览器打开“index.html”文件,即可纵览文档内容。 人性化翻译,文档中的代码和结构保持不变,注释和说明精准翻译,请放心使用。 双语对照,边学技术、边学英语。

    scala-parser-combinators_2.12-1.1.0-API文档-中英对照版.zip

    赠送jar包:scala-parser-combinators_2.12-1.1.0.jar;...使用方法:解压翻译后的API文档,用浏览器打开“index.html”文件,即可纵览文档内容。 人性化翻译,文档中的代码和结构保持不变,注释和说明精准翻

    scala-parser-combinators_2.12-1.1.0-API文档-中文版.zip

    赠送jar包:scala-parser-combinators_2.12-1.1.0...使用方法:解压翻译后的API文档,用浏览器打开“index.html”文件,即可纵览文档内容。 人性化翻译,文档中的代码和结构保持不变,注释和说明精准翻译,请放心使用。

    scalajs-types-util:Scala.js类型实用程序可促进常见类型转换

    如何使用 在您的SBT项目中添加以下行。 libraryDependencies + = " net.exoego " %%% " scalajs-types-util " % " 0.3.0 " 工厂宏 @Factory宏创建为JS性状的高度优化工厂方法,就像正常情况下的类。 每个工厂方法都...

    flink-scala-2.11-1.10.0-API文档-中文版.zip

    赠送jar包:flink-scala_2.11-1.10.0.jar;...使用方法:解压翻译后的API文档,用浏览器打开“index.html”文件,即可纵览文档内容。 人性化翻译,文档中的代码和结构保持不变,注释和说明精准翻译,请放心使用。

Global site tag (gtag.js) - Google Analytics