我正在查看http://hseeberger.wordpress.com/2010/11/25/introduction-to-category-theory-in-scala/并且有一些代码我无法理解它是如何工作的:
object Functor {
def fmap[A, B, F[_]](as: F[A])(f: A => B)(implicit functor: Functor[F]): F[B] =
functor.fmap(as)(f)
implicit object ListFunctor extends Functor[List] {
def fmap[A, B](f: A => B): List[A] => List[B] =
as => as map f
}
}
具体来说,当 的定义在范围内并且(据我所知)无法 访问时,如何ListFunctor.fmap
访问?as
as
Functor.fmap
ListFunctor.fmap
(与扭曲有关)上面定义的代码有一个先前的迭代:
trait Functor[F[_]] extends GenericFunctor[Function, Function, F] {
final def fmap[A, B](as: F[A])(f: A => B): F[B] =
fmap(f)(as)
}
object ListFunctor extends Functor[List] {
def fmap[A, B](f: A => B): List[A] => List[B] = as => as map f
}
但同样,as
似乎可以神奇地访问ListFunctor
. 我想如果我理解其中一个,我就会理解另一个。