scala - 为什么(复制)附加到 Scala 中的 Seq 被定义为 :+ and not just + as in Set and Map?

标签 scala scala-collections

Scala 的 Map 和 Set 定义了一个 +运算符返回数据结构的副本,其中附加了一个元素。 Seq 的等效运算符表示 :+ .

这种不一致有什么原因吗?

最佳答案

Map 和 Set 没有前置 ( +: ) 或附加 ( :+ ) 的概念,因为它们没有排序。要指定您使用哪一个(附加或前置),:加入。

scala> Seq(1,2,3):+4
res0: Seq[Int] = List(1, 2, 3, 4)

scala> 1+:Seq(2,3,4)
res1: Seq[Int] = List(1, 2, 3, 4)

不要被参数的顺序弄糊涂,在 Scala 中,如果方法以:it get's applied in reverse order 结尾(不是 a.method(b) 而是 b.method(a))

关于scala - 为什么(复制)附加到 Scala 中的 Seq 被定义为 :+ and not just + as in Set and Map?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12124834/

相关文章:

scala - 无法附加到 Scala 的可变 LinkedList?

java - java/scala 中的原始对象大小和 'as<SomePrimitive>Buffer() 方法调用

scala - 如何从 shell 作为普通命令行程序运行 sbt 主类?

scala - IntelliJ 1 3's compiler doesn' t 在 Play 项目中发现错误,但荧光笔确实如此

Scala重载高阶函数导致类型错误

scala - 每次需要没有重复的序列时使用 Set 而不是 Seq 是一种好习惯吗?

scala - 模拟 Scala ParStream

scala - scaladoc 说 "All operations are guaranteed to be performed in a single-threaded manner"是什么意思?

arrays - 尝试在 Scala 中声明特定大小的数组

scala - 单例对象的 protected[package] 和 private[package] 之间的区别