好吧,Scala让我感觉非常密集.我发现文档非常难以理解 - 更糟糕的是,你不能谷歌"Scala ++:"这个术语,因为谷歌放弃了运算符条款!
我正在阅读一些代码并看到这一行:
Seq(file) ++: children.flatMap(walkTree(_))
Run Code Online (Sandbox Code Playgroud)
但无法弄明白.Seq展示三件事的文档:
++
++:
++:
Run Code Online (Sandbox Code Playgroud)
后两者超负荷做什么的东西.文档中的实际解释说他们做同样的事情++.即,将一个列表添加到另一个列表.
那么,运营商之间究竟有什么区别呢?
win*_*ner 12
++并++:返回不同的结果,当操作数是不同类型的集合.++返回与左侧相同的集合类型,并++:返回与右侧相同的集合类型:
scala> List(5) ++ Vector(5)
res2: List[Int] = List(5, 5)
scala> List(5) ++: Vector(5)
res3: scala.collection.immutable.Vector[Int] = Vector(5, 5)
Run Code Online (Sandbox Code Playgroud)
有两个重载版本++:仅出于实现原因.++:需要能够采取任何TraversableOnce,但为Traversable(TraversableOnce效率)的(子类型)提供了一个重载版本.
只想确认一下:
方法名称末尾的冒号 (:) 使调用颠倒。
让我们创建两个方法,看看会发生什么:
object Test {
def ~(i: Int) = null
def ~:(i: Int) = null //putting ":" in the tail!
this ~ 1 //compiled
1 ~: this //compiled
this.~(1) //compiled
this.~:(1) //compiled.. lol
this ~: 1 //error
1 ~ this //error
}
Run Code Online (Sandbox Code Playgroud)
所以, in seq1 ++: seq2,++:实际上是seq2的方法。
编辑:正如@okiharaherbst 所提到的,这称为右结合。
| 归档时间: |
|
| 查看次数: |
1935 次 |
| 最近记录: |