我正在寻找一个Bi-Functor的标准类型类,它有一个Contravariant参数和一个Covariant参数。
打击签名(c -> a) -> (b -> d) -> f a b -> f c d
导致没有匹配。
基本上在Scala我想做:
trait CoContraBiFunctor[F[_, _]] {
def ccmap[A, B, C, D](fab: F[A, B])(f: C => A)(g: B => D): F[C, D]
}
implicit val ccFunction: CoContraBiFunctor[Function1] = new CoContraBiFunctor[Function] {
override def ccmap[A, B, C, D](fab: Function[A, B])(f: C => A)(g: B => D): Function[C, D] = new Function[C, D] {
override def apply(c: C): D = g(fab(f(c)))
}
}
有人有想法吗?我绝对不是第一个寻找这个的人。
这被称为profunctor!这是一个very useful type of bifunctor出现在各地(例如,in the construction of lenses)!在Haskell中,它可以在Data.Profunctor
包中作为profunctors
使用。我不是Scala人,但看起来它也是available中的cats
。