2016-08-25 4 views
2

私はこのようなzipWithを定義していたとします、私はエラーを得たしかしこのタイプの不一致を修正するにはどうすればよいですか?

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]]) 

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

今、私はそのような行列をビュン用に使用したいですなぜVector[Vector[Int]]Iterable[Iterable[Int]]と一致しないのですか?このエラーを修正するには?

答えて

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)) 
+0

おかげで私はより一般的な解決策を取得したいのですが。より一般的な方が良いでしょう:) – Michael

+0

[OK]をクリックすると、マトリックスタイプをより一般的に定義する必要がありますか? Matrix = Iterable [反復可能[Int]]と入力しますか?おそらく、 – Nyavro

+0

。しかし、Vector [Vector [Int]]の何が間違っていますか?私が 'Matrix'型を変更できないとします。 – Michael

関連する問題