scala中并行或顺序收集的类型

scala中并行或顺序收集的类型,scala,types,parallel-processing,Scala,Types,Parallel Processing,我有以下价值: val a = (1 to 10).toVector 并希望定义以下函数: def sum1(a: ParOrSeqVector) = a.map(_+1) 可以处理连续或并行向量 sum1(a) sum1(a.par) 我该怎么做 “扩展自”没有给我任何线索。有两种方法 1) 声明一个接受GenSeq类型参数的函数 val a: Vector[Int] = (1 to 10).toVector val a1: ParVector[Int] = ParVector.rang

我有以下价值:

val a = (1 to 10).toVector
并希望定义以下函数:

def sum1(a: ParOrSeqVector) = a.map(_+1)
可以处理连续或并行向量

sum1(a)
sum1(a.par)
我该怎么做


“扩展自”没有给我任何线索。

有两种方法

1) 声明一个接受GenSeq类型参数的函数

val a: Vector[Int] = (1 to 10).toVector
val a1: ParVector[Int] = ParVector.range(0, 10)

def sum2(a: GenSeq[Int]) = a.map(_+1)

sum2(a)
sum2(a1)
2) 接受一个函数,该函数接受任何参数,然后对其进行模式匹配

val a: Vector[Int] = (1 to 10).toVector
val a1: ParVector[Int] = ParVector.range(0, 10)

def sum1(a: Any) =
{
  a match {
    case x :Vector[Int] =>  x.map(_+1)
    case y :ParVector[Int] => y.map(_+1)
  }
}

sum1(a)
sum1(a1)
这两种情况都将以以下格式提供输出

sum2: sum2[](val a: scala.collection.GenSeq[Int]) => scala.collection.GenSeq[Int]

res2: scala.collection.GenSeq[Int] = Vector(2, 3, 4, 5, 6, 7, 8, 9, 10, 11)
res3: scala.collection.GenSeq[Int] = ParVector(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)

希望这能澄清你的问题。如果您有任何疑问,请随时询问。

谢谢,这解决了我的问题,现在我看到ParVector扩展了ParSeq,ParSeq扩展了GenSeq。