我发现了 Scala 2.9.2 ParSet zipWithIndex 的一些有趣行为,我想知道这是错误还是功能。如果答案很明显,我深表歉意,因为我是 Scala 初学者。
这是一个演示该问题的会话。
Welcome to Scala version 2.9.2 (OpenJDK 64-Bit Server VM, Java 1.7.0_21).
Type in expressions to have them evaluated.
Type :help for more information.
scala> val x = Array(1,2,3,4,5,6,7,8)
x: Array[Int] = Array(1, 2, 3, 4, 5, 6, 7, 8)
// expected behavior
scala> x.toSet
res6: scala.collection.immutable.Set[Int] = Set(5, 1, 6, 2, 7, 3, 8, 4)
scala> x.toSet.zipWithIndex
res2: scala.collection.immutable.Set[(Int, Int)] = Set((1,1), (3,5), (4,7), (5,0), (6,2), (2,3), (8,6), (7,4))
// so far so good. let's try parallel implementation
scala> x.par.toSet
res0: scala.collection.parallel.immutable.ParSet[Int] = ParSet(5, 1, 6, 2, 7, 3, 8, 4)
//
//
// UNEXPECTED BEHAVIOR HERE
//
scala> x.par.toSet.zipWithIndex
res1: scala.collection.parallel.immutable.ParSet[(Int, Int)] = ParSet((5,7), (6,5), (2,4), (1,6), (3,2), (4,0), (7,3), (8,1))
// just for good measure, this isn't an issue with Array or ParArray:
scala> x.zipWithIndex
res3: Array[(Int, Int)] = Array((1,0), (2,1), (3,2), (4,3), (5,4), (6,5), (7,6), (8,7))
scala> x.par.zipWithIndex
res7: scala.collection.parallel.mutable.ParArray[(Int, Int)] = ParArray((1,0), (2,1), (3,2), (4,3), (5,4), (6,5), (7,6), (8,7))
这是预期的行为吗?为什么 ParSet 的行为不等同于 Set?