Bi的名称 - 具有一个逆变量和一个协变参数的Functor类型类

问题描述 投票:9回答:1

我正在寻找一个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)))
  }
}

有人有想法吗?我绝对不是第一个寻找这个的人。

scala haskell functional-programming typeclass scala-cats
1个回答
11
投票

这被称为profunctor!这是一个very useful type of bifunctor出现在各地(例如,in the construction of lenses)!在Haskell中,它可以在Data.Profunctor包中作为profunctors使用。我不是Scala人,但看起来它也是available中的cats

© www.soinside.com 2019 - 2024. All rights reserved.