简体   繁体   中英

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? As far as I understand Random.shuffle(coll) creates a copy, and it is not a memory-efficient way. Is Random.shuffle(coll.view) (or coll.stream or coll.iterator) a better approach? Does this create a noticeable CPU overhead? 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).

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,

val myVector: Vector[MyObject] = ...

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

This copies a sequence of Int , likely lighter than dedicated objects. Then

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

iterates / maps over each item in myVector in a random order.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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