diff options
author | Paul Phillips <paulp@improving.org> | 2009-10-28 14:15:32 +0000 |
---|---|---|
committer | Paul Phillips <paulp@improving.org> | 2009-10-28 14:15:32 +0000 |
commit | cf53536f9e1b668e9e05c280db84ef2eb502765c (patch) | |
tree | 51bb9d177d64422c9e6b3d9c33596c595e42ecca /src | |
parent | dbdac60079bdf5cdf3cb73d161c3d605a298795e (diff) | |
download | scala-cf53536f9e1b668e9e05c280db84ef2eb502765c.tar.gz scala-cf53536f9e1b668e9e05c280db84ef2eb502765c.tar.bz2 scala-cf53536f9e1b668e9e05c280db84ef2eb502765c.zip |
The final, tear-inducingly simple implementatio...
The final, tear-inducingly simple implementation of shape preserving
shuffle.
Diffstat (limited to 'src')
-rw-r--r-- | src/library/scala/util/Random.scala | 15 |
1 files changed, 7 insertions, 8 deletions
diff --git a/src/library/scala/util/Random.scala b/src/library/scala/util/Random.scala index d22b3eca61..54feb8ac72 100644 --- a/src/library/scala/util/Random.scala +++ b/src/library/scala/util/Random.scala @@ -111,14 +111,13 @@ object Random extends Random import collection.mutable.ArrayBuffer import collection.generic.CanBuildFrom - /** Returns a new sequence in random order. - * @param seq the sequence to shuffle - * @return the shuffled sequence + /** Returns a new collection of the same type in a randomly chosen order. + * + * @param coll the Traversable to shuffle + * @return the shuffled Traversable */ - def shuffle[T, CC[_] <: Traversable[_]](seq: CC[T])(implicit bf: CanBuildFrom[CC[T], T, CC[T]]): CC[T] = { - val builder = bf(seq) - val buf = new ArrayBuffer[T] - seq foreach (x => buf += x.asInstanceOf[T]) // why is this cast necessary? + def shuffle[T, CC[X] <: Traversable[X]](coll: CC[T])(implicit bf: CanBuildFrom[CC[T], T, CC[T]]): CC[T] = { + val buf = new ArrayBuffer[T] ++ coll def swap(i1: Int, i2: Int) { val tmp = buf(i1) @@ -131,6 +130,6 @@ object Random extends Random swap(n - 1, k) } - bf(seq) ++= buf result + bf(coll) ++= buf result } } |