Scala列表串联,::: vs ++
:::
和++
之间用于串联Scala中的列表是否有区别?
Is there any difference between :::
and ++
for concatenating lists in Scala?
scala> List(1,2,3) ++ List(4,5)
res0: List[Int] = List(1, 2, 3, 4, 5)
scala> List(1,2,3) ::: List(4,5)
res1: List[Int] = List(1, 2, 3, 4, 5)
scala> res0 == res1
res2: Boolean = true
从文档中看起来++
更通用,而:::
是List
特定的.是否提供后者是因为它已用于其他功能语言?
From the documentation it looks like ++
is more general whereas :::
is List
-specific. Is the latter provided because it's used in other functional languages?
旧版. List最初被定义为具有功能性语言的外观:
Legacy. List was originally defined to be functional-languages-looking:
1 :: 2 :: Nil // a list
list1 ::: list2 // concatenation of two lists
list match {
case head :: tail => "non-empty"
case Nil => "empty"
}
当然,Scala以特别的方式发展了其他系列.当2.8发布时,对集合进行了重新设计,以实现最大的代码重用性和一致的API,以便您可以使用++
来连接 any 两个集合-甚至是迭代器.但是,除了一个或两个已弃用的列表之外,List必须保留其原始运算符.
Of course, Scala evolved other collections, in an ad-hoc manner. When 2.8 came out, the collections were redesigned for maximum code reuse and consistent API, so that you can use ++
to concatenate any two collections -- and even iterators. List, however, got to keep its original operators, aside from one or two which got deprecated.