1、存在性类型:Existential types
def foo(l: List[Option[_]]) = ... 2、高阶类型参数:Higher kinded type parameterscase class A[K[_],T](a: K[T]) 3、临时变量:Ignored variables
val _ = 5 4、临时参数:Ignored parameters
List(1, 2, 3) foreach { _ => println("Hi") } 5、通配模式:Wildcard patterns
Some(5) match { case Some(_) => println("Yes") }
match {
case List(1,_,_) => " a list with three element and the first element is 1"
case List(_*) => " a list with zero or more elements "
case Map[_,_] => " matches a map with any key type and any value type "
case _ =>
}
val (a, _) = (1, 2)
for (_ <- 1 to 10) 6、通配导入:Wildcard imports
import java.util._ 7、隐藏导入:Hiding imports
// Imports all the members of the object Fun but renames Foo to Barimport com.test.Fun.{ Foo => Bar , _ } // Imports all the members except Foo. To exclude a member rename it to _import com.test.Fun.{ Foo => _ , _ } 8、连接字母和标点符号:Joining letters to punctuation
def bang_!(x: Int) = 5 9、占位符语法:Placeholder syntax
List(1, 2, 3) map (_ + 2)
_ + _
( (_: Int) + (_: Int) )(2,3) val nums = List(1,2,3,4,5,6,7,8,9,10) nums map (_ + 2)
nums sortWith(_>_)
nums filter (_ % 2 == 0)
nums reduceLeft(_+_)
nums reduce (_ + _)
nums reduceLeft(_ max _)
nums.exists(_ > 5)
nums.takeWhile(_ < 8) 10、偏应用函数:Partially applied functions
def fun = {
// Some code
}
val funLike = fun _ List(1, 2, 3) foreach println _ 1 to 5 map (10 * _) //List("foo", "bar", "baz").map(_.toUpperCase())List("foo", "bar", "baz").map(n => n.toUpperCase()) 11、初始化默认值:default value
var i: Int = _ 12、作为参数名:
//访问mapvar m3 = Map((1,100), (2,200))
for(e<-m3) println(e._1 + ": " + e._2)
m3 filter (e=>e._1>1)
m3 filterKeys (_>1)
m3.map(e=>(e._1*10, e._2))
m3 map (e=>e._2) //访问元组:tuple getters
(1,2)._2 13、参数序列:parameters Sequence
_*作为一个整体,告诉编译器你希望将某个参数当作参数序列处理。例如val s = sum(1 to 5:_*)就是将1 to 5当作参数序列处理。
//Range转换为ListList(1 to 5:_*) //Range转换为VectorVector(1 to 5: _*) //可变参数中def capitalizeAll(args: String*) = {
args.map { arg =>
arg.capitalize
}
} val arr = Array("what's", "up", "doc?")
capitalizeAll(arr: _*)

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

foo _ // Eta expansion of method into method value

foo(_) // Partial function application

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

trait PlaceholderExample {
def process[A](f: A => Unit) val set: Set[_ => Unit] set.foreach(process _) // Error
set.foreach(process(_)) // No Error
}
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.

最新文章

  1. CSS制作凹环特效
  2. Android—Bundle传递ArrayList&lt;T&gt;
  3. javascript原型方法
  4. Java问题总结
  5. 【leetcode】Palindrome Number (easy)
  6. HTTP协议 Servlet入门 Servlet工作原理和生命周期 Servlet细节 ServletConfig对象
  7. [LeetCode] 237. Delete Node in a Linked List 解题思路
  8. [置顶] gis海量资源网盘提供VIP账号无广告高速下载 (更新更多资源)
  9. JRE与JDK
  10. javaScript(5)---运算符
  11. LeetCode 字符串专题(一)
  12. TensorFlow下利用MNIST训练模型并识别自己手写的数字
  13. 谈谈 JavaScript 的正则表达式
  14. 5.5 C++重载赋值操作符
  15. spring@Transactional注解事务不回滚不起作用无效的问题处理
  16. jenkins slave 安装服务与卸载
  17. Python3基础 __setattr__ 在属性被赋值的时候,新增提示功能
  18. GitHub正式启用声明
  19. 已有iptables表的查看
  20. WPF 和 UWP 中,不用设置 From 或 To,Storyboard 即拥有更灵活的动画控制

热门文章

  1. 动态导入模块__import__(&quot;str&quot;) importlib标准库
  2. libevent源码学习(9):事件event
  3. Various methods for capturing the screen
  4. video标签实现多个视频循环播放
  5. windows平台使用 pthreads库
  6. Linux蓝牙库blueZ
  7. Java代码性能优化
  8. EMA
  9. 对vector和map容器的删除元素操作
  10. Linux 离奇磁盘爆满解决办法