如何解决此类型不匹配问题?

时间:2016-08-25 15:04:13

标签: scala collections iterable

假设我这样定义zipWith

def zipWith[A](f:(A, A) => A)(xs:Iterable[A], ys:Iterable[A]): Iterable[A] =
  (xs, ys).zipped map f

现在我想用于这样的压缩矩阵:

type Matrix = Vector[Vector[Int]]

val zipMatrix: ((Int, Int) => Int) => Matrix => Matrix => Matrix = f =>
  zipWith(zipWith(f)) 

但是我收到了一个错误:

<console>:15: error: type mismatch;
found   : (Iterable[Iterable[Int]], Iterable[Iterable[Int]]) => Iterable[Iterable[Int]]
required: Matrix => (Matrix => Matrix)
(which expands to)  scala.collection.immutable.Vector[Vector[Int]] => (scala.collection.immutable.Vector[Vector[Int]] => scala.collection.immutable.Vector[Vector[Int]])

为什么Vector[Vector[Int]]Iterable[Iterable[Int]]匹配?如何解决此错误?

1 个答案:

答案 0 :(得分:1)

考虑一下:

def zipWith[A] (f:(A => A => A))(xs:Vector[A])(ys:Vector[A]): Vector[A] =
  (xs, ys).zipped.map {case (x, y) => f (x)(y)}

val zipMatrix: (Int => Int => Int) => (Matrix => Matrix => Matrix) = f =>
  zipWith(zipWith(f))