收集:scala语法用例和疑难点
update: 2017-07-15
A. 语法
method vs function
// method
scala> def m(x: Int, y: Int): Int = x + y
res1: m: (x: Int, y: Int)Int
// function
scala> val f = (x: Int, y: Int) => x + y
res2: f: (Int, Int) => Int = <function2>
// method to function
scala> m _
res3: (Int, Int) => Int = <function2>
// curried
scala> f.curried
res4: Int => (Int => Int) = <function1>
scala> (m _).curried
res5: Int => (Int => Int) = <function1>
传参时,每个_
对应一个参数:
// (1) wrong
List(1, 2, 3).map(x => x + 1)
List(1, 2, 3).map(_ => _ + 1) // won't compile
// = List(1, 2, 3).map(x => {y => y + 1})
// (2) right
List(1, 2, 3, 4).reduce((x, y) => x + y)
// is equivalent to
List(1, 2, 3, 4).reduce(_ + _)
参考:In Scala, what is the difference between using the _
and using a named identifier?
Implicit
B. 用例
遍历Option[Array[A]]类型
val oas: Option[Array[Int]] = Some(Array(1, 2, 3))
val res =
for {
as <- oas.toArray // or use .toSeq
a <- as
} yield a + 1
这里之所以要使用.toArray变换,是为了使外层的Option.flatMap转成Array.flatMap,从而和内层的Array.map类型兼容。
参考:For comprehension over Option array
函数用元组做为参数列表
scala> def f(i : Int, j : Int) = i + j
scala> val args = (0, 1)
scala> val tf = (f _).tupled
tf: ((Int, Int)) => Int = <function1>
scala> tf(args)
res7: Int = 1