在阅读函子的描述在此博客:Scala - 如何在非函数类型上使用函子?
https://hseeberger.wordpress.com/2010/11/25/introduction-to-category-theory-in-scala/
有函子的一个通用的定义,更具体的一个:
trait GenericFunctor[->>[_, _], ->>>[_, _], F[_]] {
def fmap[A, B](f: A ->> B): F[A] ->>> F[B]
}
trait Functor[F[_]] extends GenericFunctor[Function, Function, F] {
final def fmap[A, B](as: F[A])(f: A => B): F[B] =
fmap(f)(as)
}
显然,这意味着函子可以与其它用于除函数对象外还有更高级的类型。有人可以举一个例子或解释如何或为什么或在什么情况下会做?也就是说,GenericFunctor的另一个实现是在Scala中使用了一个与Function不同的类型构造函数?谢谢!
编辑:
只是为了澄清:
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
}
}
scala> fmap(List(1, 2, 3))(x => x + 1)
res0: List[Int] = List(2, 3, 4)
只是为了澄清,根据我的理解ListFunctor实现了GenericFunctor 1-ARG FMAP而在REPL成绩单代码调用特质的FMAP Functor,然后调用fmap实现(例如在ListFunctor中)。
这并没有改变整体问题,只是认为它会帮助人们试图提供答案。任何见解提供将不胜感激。
我不认为这两个函子是一个仿函数。我看不出你将如何实施任一类别。 – Anonymous