简体   繁体   English

Scala:以随机顺序遍历集合,而无需创建副本

[英]Scala: iterate over collection in random order without creating a copy

Is there a way in Scala to obtain a stream/view/iterator for an immutable collection (eg List or Vector) which will traverse the collection in random order? Scala中是否有一种方法可以获取不可变集合(例如List或Vector)的流/视图/迭代器,该不可变集合将以随机顺序遍历该集合? As far as I understand Random.shuffle(coll) creates a copy, and it is not a memory-efficient way. 据我了解,Random.shuffle(coll)创建一个副本,但这不是一种节省内存的方法。 Is Random.shuffle(coll.view) (or coll.stream or coll.iterator) a better approach? 是Random.shuffle(coll.view)(还是coll.stream或coll.iterator)更好的方法? Does this create a noticeable CPU overhead? 这会造成明显的CPU开销吗? Or is there some more appropriate way? 还是有一些更合适的方法?

Shuffling algorithms need to move randomly around in the collection and need to remember past choices somehow. 改组算法需要在集合中随机移动,并且需要以某种方式记住过去的选择。 The immutable collections are not very speedy with random access ( O(n) instead of O(1) for List , arguably O(1) for Vector but the constant factor is large). 不可变集合使用随机访问的速度不是很快O(1) List O(n)而不是O(1)Vector O(1)可以说是O(1) ,但是常数因子很大)。

Copying the collection, in comparison, is almost always the wise thing to do. 相比之下,复制收藏几乎总是明智之举。

If collections copying is taken into account, consider shuffling an Array of indices to a Vector of interest, for instance like this, 如果考虑到集合复制,请考虑将索引Array改组为感兴趣的Vector ,例如这样,

val myVector: Vector[MyObject] = ...

val shuffledIdx = util.Random.shuffle(0 until myVector.size)

This copies a sequence of Int , likely lighter than dedicated objects. 这将复制一个Int序列,该序列可能比专用对象 Then 然后

shuffledIdx.map { idx => task (myVector(idx)) }

iterates / maps over each item in myVector in a random order. 以随机顺序迭代/映射myVector中的每个项目。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM