Bi 的名称 - 具有一个逆变和一个协变参数的函子类型 class

Name of Bi - Functor type class with one contravariant and one covariant parameter

我正在寻找具有一个逆变参数和一个协变参数的 Bi-Functor 的标准类型类。

打孔签名 (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!它也是 cats 中的 very useful type of bifunctor that shows up all over the place (for example, in the construction of lenses)! In Haskell it is available as Data.Profunctor in the profunctors package. I am not a Scala person but it looks like it is available