我具有以下特征...
import cats._
import cats.implicits._
trait Preference[-A] {
self =>
def compare(a1: A, a2: A): Int
final def ordering[A1 <: A]: Ordering[A1] = {
new Ordering[A1] {
def compare(a1: A1, a2: A1): Int = {
self.compare(a1, a2)
}
}
}
}
object Preference {
implicit val contravariant: Contravariant[Preference] = {
new Contravariant[Preference] {
def contramap[A, B](fa: Preference[A])(f: B => A): Preference[B] = {
new Preference[B] {
def compare(b1: B, b2: B): Int = {
fa.compare(f(b1), f(b2))
}
}
}
}
}
}
我想为此特性定义Apply
,Applicative
,甚至可能定义Monad
实例,但是所有这些类型类都是Functor
的扩展。这些类型类的版本在Cats中是否存在逆函子?
答案 0 :(得分:2)
Haskell中wpdb
的对等等效项是Divisible
和Applicative
allows to define both of its methods (divide
is contramap2
, conquer
is trivial
)。不过,我不确定是否可以肯定cats.ContravariantMonoidal
是猫的Divisible
。
对于Monoidal
,Kmett说