实施Applicative#产品

时间:2014-01-15 04:25:22

标签: scala

使用Functional Programming in Scala中的Applicative#product,这是我的map2代码

 def product[G[_]](G: Applicative[G]):
        Applicative[({type f[x] = (F[x], G[x])})#f] = {
    val self = this

[omitted 'unit' and 'apply' function definitions]

       override def map2[A,B,C](fa: (F[A], G[A]), fb: (F[B], G[B]))
                              (f: (A,B) => C): (F[C], G[C]) = {

        val x: (F[A], F[B]) = (fa._1, fb._1)
        val y: (G[A], G[B]) = (fa._2, fb._2)
        val f: F[C] = map(x)(f.tupled)
        val g: G[C] = map(y)(f.tupled)
        (f, g)
      }
    }
  }

但是,我不知道为什么map参数需要(F[?], G[?])类型。这是为什么?

[error] C:\Users\Kevin\...\Applicative.scala:66: type mismatch;
[error]  found   : (F[A], F[B])
[error]  required: (F[?], G[?])
[error]         val f: F[C] = map(x)(f.tupled)
[error]                           ^
[error] C:\Users\Kevin\...\Applicative.scala:67: type mismatch;
[error]  found   : (G[A], G[B])
[error]  required: (F[?], G[?])
[error]         val g: G[C] = map(y)(f.tupled)
[error]                           ^
[error] two errors found

0 个答案:

没有答案