1. 1、存在性类型:Existential types
  2. def foo(l: List[Option[_]]) = ...
  3.  
  4. 2、高阶类型参数:Higher kinded type parameterscase class A[K[_],T](a: K[T])
  5.  
  6. 3、临时变量:Ignored variables
  7. val _ = 5
  8.  
  9. 4、临时参数:Ignored parameters
  10. List(1, 2, 3) foreach { _ => println("Hi") }
  11.  
  12. 5、通配模式:Wildcard patterns
  13. Some(5) match { case Some(_) => println("Yes") }
  14. match {
  15. case List(1,_,_) => " a list with three element and the first element is 1"
  16. case List(_*) => " a list with zero or more elements "
  17. case Map[_,_] => " matches a map with any key type and any value type "
  18. case _ =>
  19. }
  20. val (a, _) = (1, 2)
  21. for (_ <- 1 to 10)
  22.  
  23. 6、通配导入:Wildcard imports
  24. import java.util._
  25.  
  26. 7、隐藏导入:Hiding imports
  27. // Imports all the members of the object Fun but renames Foo to Barimport com.test.Fun.{ Foo => Bar , _ }
  28.  
  29. // Imports all the members except Foo. To exclude a member rename it to _import com.test.Fun.{ Foo => _ , _ }
  30.  
  31. 8、连接字母和标点符号:Joining letters to punctuation
  32. def bang_!(x: Int) = 5
  33.  
  34. 9、占位符语法:Placeholder syntax
  35. List(1, 2, 3) map (_ + 2)
  36. _ + _
  37. ( (_: Int) + (_: Int) )(2,3)
  38.  
  39. val nums = List(1,2,3,4,5,6,7,8,9,10)
  40.  
  41. nums map (_ + 2)
  42. nums sortWith(_>_)
  43. nums filter (_ % 2 == 0)
  44. nums reduceLeft(_+_)
  45. nums reduce (_ + _)
  46. nums reduceLeft(_ max _)
  47. nums.exists(_ > 5)
  48. nums.takeWhile(_ < 8)
  49.  
  50. 10、偏应用函数:Partially applied functions
  51. def fun = {
  52. // Some code
  53. }
  54. val funLike = fun _
  55.  
  56. List(1, 2, 3) foreach println _
  57.  
  58. 1 to 5 map (10 * _)
  59.  
  60. //List("foo", "bar", "baz").map(_.toUpperCase())List("foo", "bar", "baz").map(n => n.toUpperCase())
  61.  
  62. 11、初始化默认值:default value
  63. var i: Int = _
  64.  
  65. 12、作为参数名:
  66. //访问mapvar m3 = Map((1,100), (2,200))
  67. for(e<-m3) println(e._1 + ": " + e._2)
  68. m3 filter (e=>e._1>1)
  69. m3 filterKeys (_>1)
  70. m3.map(e=>(e._1*10, e._2))
  71. m3 map (e=>e._2)
  72.  
  73. //访问元组:tuple getters
  74. (1,2)._2
  75.  
  76. 13、参数序列:parameters Sequence
  77. _*作为一个整体,告诉编译器你希望将某个参数当作参数序列处理。例如val s = sum(1 to 5:_*)就是将1 to 5当作参数序列处理。
  78. //Range转换为ListList(1 to 5:_*)
  79.  
  80. //Range转换为VectorVector(1 to 5: _*)
  81.  
  82. //可变参数中def capitalizeAll(args: String*) = {
  83. args.map { arg =>
  84. arg.capitalize
  85. }
  86. }
  87.  
  88. val arr = Array("what's", "up", "doc?")
  89. capitalizeAll(arr: _*)

这里需要注意的是,以下两种写法实现的是完全不一样的功能:

  1. foo _ // Eta expansion of method into method value
  2.  
  3. foo(_) // Partial function application

Example showing why foo(_) and foo _ are different:

  1. trait PlaceholderExample {
  2. def process[A](f: A => Unit)
  3.  
  4. val set: Set[_ => Unit]
  5.  
  6. set.foreach(process _) // Error
  7. set.foreach(process(_)) // No Error
  8. }
In the first case, process _ represents a method; Scala takes the polymorphic method and attempts to make it monomorphic by filling in the type parameter, but realizes that there is no type that can be filled in for A that will give the type (_ => Unit) => ? (Existential _ is not a type).
In the second case, process(_) is a lambda; when writing a lambda with no explicit argument type, Scala infers the type from the argument that foreach expects, and _ => Unit is a type (whereas just plain _ isn't), so it can be substituted and inferred.
 
This may well be the trickiest gotcha in Scala I have ever encountered.

Scala 中下划线的用法的更多相关文章

  1. Scala 中下划线的用途

    转载自:https://my.oschina.net/leejun2005/blog/405305 Scala 作为一门函数式编程语言,对习惯了指令式编程语言的同学来说,会不大习惯,这里除了思维方式之 ...

  2. 浅谈 Scala 中下划线的用途

    Scala 作为一门函数式编程语言,对习惯了指令式编程语言的同学来说,会不大习惯,这里除了思维方式之外,还有语法层面的,比如 underscore(下划线)就会出现在多种场合,令初学者相当疑惑,今天就 ...

  3. 转载:浅谈 Scala 中下划线的用途

    Scala 作为一门函数式编程语言,对习惯了指令式编程语言的同学来说,会不大习惯,这里除了思维方式之外,还有语法层面的,比如 underscore(下划线)就会出现在多种场合,令初学者相当疑惑,今天就 ...

  4. scala下划线的用法

    1.作为“通配符”,类似Java中的*.如import scala.math._2.:_*作为一个整体,告诉编译器你希望将某个参数当作参数序列处理!例如val s = sum(1 to 5:_*)就是 ...

  5. Django中下划线的用法介绍(一)

    在Django中有相当多的操作是通过双下划线与动作连接起来使用,为了以后更加方便的查找和使用,现在总结以下Django中基本的双下划线操作 比较符:大于--gt  小于--lt 等于--eq  大于等 ...

  6. Scala中下划线的总结

    1. 方法转化为函数 2. 集合中的每一个元素 3. 获取元组Tuple中的元素 4. 模式匹配 5. 队列 6. 导包引入的时候 7. 初始化变量 引用自:https://blog.csdn.net ...

  7. Python中下划线---完全解读(转)

      Python中下划线---完全解读 Python 用下划线作为变量前缀和后缀指定特殊变量 _xxx 不能用’from module import *’导入 __xxx__ 系统定义名字 __xxx ...

  8. Python中下划线的使用方法

    本文将讨论Python中下划线(_)字符的使用方法.我们将会看到,正如Python中的很多事情,下划线的不同用法大多数(并非所有)只是常用惯例而已. 单下划线(_) 通常情况下,会在以下3种场景中使用 ...

  9. Scala进阶之路-Scala中的枚举用法案例展示

    Scala进阶之路-Scala中的枚举用法案例展示 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. Scala中的枚举值和Java中的枚举值有点差别,不过使用起来也都差大同小异,我这 ...

随机推荐

  1. ymal文档格式 处理

    Python也可以很容易的处理ymal文档格式,只不过需要安装一个模块. 参考文档:http://pyyaml.org/wiki/PyYAMLDocumentation

  2. github访问慢处理办法

    Windows 系统:C:\Windows\System32\drivers\etc\hostsLinux 系统:/etc/hostsMac(苹果电脑)系统:/etc/hostsAndroid(安卓) ...

  3. UDP&串口调试助手用法(1)

    一览 UDP 串口 常用 功能概述 概览 支持UDP通信协议: 广播.单播.组播 支持串口通信 配置了常用的配置,常用的进制转化: 2进制,8进制,10进制,和16进制之间的转换 配置了 计算器,加减 ...

  4. Android JNI 启动线程,并设置线程名称

    p.p1 { margin: 0; font: 12px Menlo; color: rgba(100, 56, 32, 1); background-color: rgba(255, 255, 25 ...

  5. 【九度OJ】题目1207:质因数的个数 解题报告

    [九度OJ]题目1207:质因数的个数 解题报告 标签(空格分隔): 九度OJ 原题地址:http://ac.jobdu.com/problem.php?pid=1207 题目描述: 求正整数N(N& ...

  6. 【LeetCode】358. Rearrange String k Distance Apart 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址: https://leetcode.com/problems/rearrang ...

  7. 【LeetCode】200. Number of Islands 岛屿数量

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 DFS BFS 日期 题目地址:https://le ...

  8. 【LeetCode】423. Reconstruct Original Digits from English 解题报告(Python)

    [LeetCode]423. Reconstruct Original Digits from English 解题报告(Python) 标签: LeetCode 题目地址:https://leetc ...

  9. JS常用的获取值和设值的方法

    1. input 标签<input type="text" name="username" id="name"/> 1) 获取i ...

  10. Unsupervised Feature Learning via Non-Parametric Instance Discrimination

    目录 概 主要内容 Wu Z., Xiong Y., Yu S. & Lin D. Unsupervised Feature Learning via Non-Parametric Insta ...