【问题标题】:Shuffle some sequence of List in Scala在 Scala 中打乱一些 List 序列
【发布时间】:2017-03-05 06:07:01
【问题描述】:

我正在使用scala.util.Random,然后我知道如何随机播放列表。在 Array 列表中随机化一个顺序非常有用,例如:

val List(a, b, c, d) = Random.shuffle(List(1,2,3,4))
it will result

a = 1
b = 3
c = 2
d = 4

a、b、c、d 的结果可以是 1 到 4 之间的任意随机值。 但问题是我们不能这样做:

val a:List = List(1,2,Random.shuffle(3,4,5))

如果你们对此案有任何线索,请分享。如果不是,请解释原因。

谢谢!

【问题讨论】:

  • 你对1 :: 2 :: Random.shuffle(List(3,4,5))有什么意见?

标签: scala list random sequence shuffle


【解决方案1】:

val a:List = List(1,2,Random.shuffle(3,4,5))这行会给出类型错误

原因

Shuffle 不采用 var args 并且 List apply 方法也不会同时将元素和 List 作为参数。这就是标准库中 shuffle 的声明方式。

 def shuffle[T, CC[X] <: TraversableOnce[X]](xs: CC[T])(implicit bf: CanBuildFrom[CC[T], T, CC[T]]): CC[T] = {
    val buf = new ArrayBuffer[T] ++= xs

    def swap(i1: Int, i2: Int) {
      val tmp = buf(i1)
      buf(i1) = buf(i2)
      buf(i2) = tmp
    }

    for (n <- buf.length to 2 by -1) {
      val k = nextInt(n)
      swap(n - 1, k)
    }

    (bf(xs) ++= buf).result()
  }

改为这样做

val a = List(1, 2) ++ Random.shuffle(List(3, 4, 5))

如果你想要更友好的 API,只需声明一个这样的隐式

implicit class ListUtils[A](list: List[A]) {
  import scala.util.Random
  def addShuffling(xs: A*) = list ++ Random.shuffle(xs.toList)
}

Scala REPL

scala> import scala.util._

scala>  implicit class ListUtils[A](list: List[A]) {
     |       def addShuffling(xs: A*) = list ++ Random.shuffle(xs.toList)
     |     }
defined class ListUtils

scala> List(1, 2).addShuffling(3, 4, 5)
res3: List[Int] = List(1, 2, 5, 3, 4)

对于长列表,Vector 会更好。对长列表使用向量实现

implicit class VectorUtils[A](vec: Vector[A]) {
   def addShuffling(xs: A*) = vec ++ Random.shuffle(xs.toVector)
}

Scala REPL

implicit class VectorUtils[A](vec: Vector[A]) {
      import scala.util.Random
      def addShuffling(xs: A*) = vec ++ Random.shuffle(xs.toVector)
    }

    Vector(1, 2).addShuffling(3, 4, 5)

// Exiting paste mode, now interpreting.

defined class VectorUtils
res1: scala.collection.immutable.Vector[Int] = Vector(1, 2, 3, 4, 5)

【讨论】:

  • 这是对的,所以原因是因为Random.shuffle只与List关联?
  • @DedenBangkit 用隐含的想法编辑了答案.. 只需检查
  • 当然@pamu,隐含的想法太天才了!谢谢!你刚刚回答了我的问题。
  • @DedenBangkit 编辑了答案请检查。使用向量而不是列表。向量追加比列表追加更有效
  • @DedenBangkit no... 附加到列表是O(n) 操作,但附加到向量是O(1) 操作
猜你喜欢
  • 2015-03-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-12-15
  • 2015-12-15
  • 1970-01-01
  • 2015-09-21
  • 1970-01-01
相关资源
最近更新 更多