Scala列表串联,::: vs ++ [英] Scala list concatenation, ::: vs ++

查看:60
本文介绍了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.

这篇关于Scala列表串联,::: vs ++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆