2017-05-04 80 views
0

我有以下示例函子:为什么解决不了的类型

trait Functor[F[_]] { 
    def map[A, B](fa: F[A])(f: A => B): F[B] 
} 

object Functor { 
    implicit val listFunctor: Functor[List] = new Functor[List] { 
    def map[A, B](fa: List[A])(f: (A) => B): List[B] = fa.map(f) 
    } 
} 

Functor.listFunctor.map(List(1,2,3,4))(_ + _) 

编译器抱怨上最后一行:

Error:(29, 47) missing parameter type for expanded function ((x$1: <error>, x$2) => x$1.$plus(x$2)) 
Functor.listFunctor.map(List(1,2,3,4))(_ + _) 
            ^

我在做什么错?

+0

“映射”定义从A到B有一个函数,您使用的是具有两个参数的部分函数,​​例如, '(_.toString)'会起作用。 –

+0

我想为他们自己添加一个,例如1 + 1,2 + 2 ...... –

回答

2

_ + _是一个带两个参数并返回它们的和的函数,这不是map所期望的。请尝试使用以下代码:

Functor.listFunctor.map(List(1,2,3,4))(i => i + i)