假设我这样定义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]]
匹配?如何解决此错误?
答案 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))