问题描述
通常(到目前为止,我通常)尝试在Scala中使用不可变集合,尤其是如果我在程序的其他部分引用集合的引用时,我不会偶然覆盖原始源.我想知道:为什么决定让DenseVector成为可变集合?
Usually (so far always) I try to use immutable collection in Scala, especially so that if I give a reference to a collection to some other part of my program I cannot override the original source by accident. Using breeze, I would like to know: Why was it decided to have DenseVector be a mutable collection?
这只是在后台在Array中使用的副作用(也许是有害的)吗?如果是这样,为什么使用数组而不是另一个(不可变的)集合?
Is this just a (maybe unwanted) side effect of using in Arrays in the background? If so, why were Arrays used, instead of another (immutable) collection?
推荐答案
-
性能.
Performance.
由除数组以外的任何内容支持的DenseVector
会慢得多.它可以包装一个ImmutableArray
并包装一个Array
,但这将强制执行某些操作,这些操作可以通过就地复制来避免复制,或者与专门化功能怪异地交互,等等.
A DenseVector
backed by anything other than an array would be substantially slower. It could wrap an ImmutableArray
which wraps an Array
, but this would force some operations which can avoid copies by being in-place to copy, maybe interact weirdly with specialization, etc.
我不知道这有多重要(我怀疑不多),但是对于从数值计算(而不是从Scala到Breeze)的人们来说,期望可变性.例如.这样可以更轻松地将在Matlab或R中实现的算法移植到Breeze.
I don't know how important this is (I suspect not much), but for people coming to Breeze from numerical computing (instead of from Scala) mutability is expected. E.g. it makes it simpler to port an algorithm which is implemented in Matlab or in R to Breeze.
这篇关于为什么DenseVector是可变的集合?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!