问题描述
有人可以向我解释 contramap
吗?这个实现会是什么样子?好的使用示例应该是什么样的?
Can someone explain contramap
to me? What would this implementation look like? What would good examples of usage look like?
// contravariant functor
trait Contravariant[F[_]] {
def contramap[A, B](f: B => A): F[A] => F[B]
}
来源:http://tmorris.net/posts/functors-and-things-using-scala/index.html
推荐答案
如果看下面的Ordering.on
标准库方法:
If you look at the following Ordering.on
method of the standard library:
def on[U](f: U => T): Ordering[U]
您将看到 on
将 Ordering[T]
转换为 Ordering[U]
同时从 获取函数U
到 T
.所以方法 on
见证了一个事实,Ordering
可以被看作是一个 Contravariant
函子:
You'll see that on
transforms an Ordering[T]
into an Ordering[U]
while taking a function from U
to T
. So the method on
witnesses the fact that Ordering
can be seen as a Contravariant
functor with:
def contramap[A, B](f: B => A) = (fa: Ordering[A]) => fa.on(f)
我还看到了 Tony 的博文,它帮助我终于理解了这个来自retronym 的三年历史answer我的问题之一.
I also saw the blog post from Tony and it helped me finally makes sense of this three year old answer from retronym to one of my question.
这篇关于解释 Contramap的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!