为什么在Scala中不赞成使用List的+运算符? [英] Why is the + operator for List deprecated in Scala?
问题描述
为什么在Scala中不建议使用List的+运算符?
Why is the + operator for List deprecated in Scala?
http://www .scala-lang.org/docu/files/api/scala/List.html#%2B%28B%29
推荐答案
很好的问题,所以我在Odersky等人的书中进行了查找.它说以下内容(希望在这里引用它不是侵犯版权;-)):
Good question, so I looked it up in the book by Odersky et al. It says the following (hopefully it's not a copyright infringement to quote this here ;-)):
类List
不提供追加操作,因为追加到列表所需的时间随列表的大小线性增长,而加上::
则需要固定的时间.如果您想通过添加元素来构建列表,则可以选择将它们放在前面,然后在完成后调用reverse
;否则,请执行以下步骤.或使用ListBuffer
(确实提供附加操作的可变列表),并在完成后调用toList
.
Class List
does not offer an append operation because the time it takes to append to a list grows linearly with the size of the list, whereas prepending with ::
takes constant time. Your options if you want to build a list by appending elements is to prepend them, then when you're done call reverse
; or use a ListBuffer
, a mutable list that does offer an append operation, and when you're done call toList
.
据我了解FP,至少在纯函数语言中,在列表之前添加列表比添加列表更为常见.我只能假定Scala的设计者添加了+
运算符,以方便Java开发人员,因为他们习惯于使用add()
进行追加,然后对此有了新的想法.
As far as I understand FP, prepending to a list is much more common than appending, at least in pure functional languages. I can only assume that the designers of Scala added the +
operator as a convenience for Java developers, who are used to appending using add()
, and then had second thoughts about it.
这篇关于为什么在Scala中不赞成使用List的+运算符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!