2017-05-04 79 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)