List scala-无法解析符号::

List scala-无法解析符号::,list,scala,List,Scala,我是Scala新手,对为什么string::List()有效有一些疑问List()::s无效?我还想知道ListBuffer是否比:更好 val columnNames :List[String] = ['foo','bar'] val work :List[String] = List() for (s <- columnNames) { s match { //this doesn't compile //case workPattern => work

我是Scala新手,对为什么
string::List()
有效有一些疑问
List()::s
无效?我还想知道ListBuffer是否比
更好

val columnNames :List[String] = ['foo','bar']
val work :List[String] = List()
for (s <- columnNames) {
  s match {
     //this doesn't compile
     //case workPattern => work :: s
     //this works        
     case workPattern => s :: work
     // this also works
     case workPattern => work :: List(s)
}
val columnNames:List[String]=['foo','bar']
val工作:列表[字符串]=列表()

for(s
a::b
字面意思是“在列表
b
开头添加元素
a
”。 它创建了一个新列表,其中
a
作为头,
b
作为尾

要将元素附加到列表中,可以使用
++
或者类似的事情
work::“foo::Nil
,后者当然不是很有效

对于问题的第二部分,如文件中所述:

Time: List has O(1) prepend and head/tail access. 
Most other operations are O(n) on the number of elements in the list. 
This includes the index-based lookup of elements, length, append and reverse.
Space: List implements structural sharing of the tail list. 
This means that many operations are either zero- or constant-memory cost.

因此,它取决于您需要执行的操作的大小和类型,这在性能方面是比较好的。

a::b
字面意思是“在列表的开头添加元素
a
”。 它创建了一个新列表,其中
a
作为头,
b
作为尾

要将元素附加到列表中,可以使用
++
或者类似的事情
work::“foo::Nil
,后者当然不是很有效

对于问题的第二部分,如文件中所述:

Time: List has O(1) prepend and head/tail access. 
Most other operations are O(n) on the number of elements in the list. 
This includes the index-based lookup of elements, length, append and reverse.
Space: List implements structural sharing of the tail list. 
This means that many operations are either zero- or constant-memory cost.

因此,它取决于您需要执行的操作的大小和类型,这在性能方面是比较好的。

s::work
中调用的函数是。Scala只是Java字符串的别名,因此不公开诸如
::
s::work
等操作符等同于
work:(


有关更多信息,您可以参考伟大的Alvin Alexander和。

s::work
中调用的函数是。Scala只是Java字符串的别名,因此不公开诸如
::
s::work
之类的运算符,它等同于
work:(s)

欲了解更多信息,请参考伟大的阿尔文·亚历山大的著作