++: 运算符对列表有什么作用? [英] What does the ++: operator do to a list?
问题描述
好吧,Scala 让我感觉很密集.我发现这些文档非常难以理解——更糟糕的是,你不能用谷歌搜索Scala ++:"这个词,因为谷歌放弃了运营商术语!
Alright, Scala has me feeling pretty dense. I'm finding the docs pretty impenetrable -- and worse, you can't Google the term "Scala ++:" because Google drops the operator terms!
我正在阅读一些代码并看到这一行:
I was reading some code and saw this line:
Seq(file) ++: children.flatMap(walkTree(_))
但是想不通.Seq
的文档显示了三件事:
But couldn't figure it out. The docs for Seq
show three things:
++
++:
++:
后两者超负荷做..某事.文档中的实际解释说它们与 ++
做同样的事情.即,将一个列表添加到另一个列表.
Where the latter two are over loaded to do.. something. The actual explanation in the doc says that they do the same thing as ++
. Namely, add one list to another.
那么,运算符之间到底有什么区别..?
So, what exactly is the difference between the operators..?
推荐答案
++
和 ++:
当操作数是不同类型的集合时返回不同的结果.++
返回与左侧相同的集合类型,++:
返回与右侧相同的集合类型:
++
and ++:
return different results when the operands are different types of collection. ++
returns the same collection type as the left side, and ++:
returns the same collection type as the right side:
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)
++:
有两个重载版本,只是为了实现的原因.++:
需要能够接受任何TraversableOnce
,但是为Traversable
(TraversableOnce
) 以提高效率.
There are two overloaded versions of ++:
solely for implementation reasons. ++:
needs to be able to take any TraversableOnce
, but an overloaded version is provided for Traversable
(a subtype of TraversableOnce
) for efficiency.
这篇关于++: 运算符对列表有什么作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!