[英]What does the map function in the Applicative from Category theory do?
Given the following statement about functional programming - in particular reasoning about category theory - we see the map
function inside the Applicative
. 给定以下有关函数式编程的说明-特别是有关类别理论的推理- 我们将在Applicative
看到 map
函数。
trait Applicative[F[_]] extends Functor[F] {
def map2[A,B,C](fa: F[A], fb: F[B])(f: (A, B) => C): F[C] =
apply(map(fa)(f.curried))(fb)
...
My question is: What does the map function in the Applicative from Category theory do? 我的问题是: “分类理论”中的“应用程序”中的地图功能是什么?
It's defined just a few lines after map2
, using only apply
and unit
: 它的定义后,短短几行map2
,只使用apply
和unit
:
def map[A,B](fa: F[A])(f: A => B): F[B] = apply(unit(f))(fa)
You can plug in the definition of map
into map2
and thereby obtain a definition of map2
that relies only on apply
and unit
: 您可以将map
的定义插入map2
,从而获得仅依赖apply
和unit
的map2
定义:
def map2[X, Y, Z](x: F[X], y: F[Y])(f: (X, Y) => Z): F[Z] =
apply(apply(unit((x: X) => (y: Y) => f(x, y)))(x))(y)
Therefore, map
is not required to define map2
from apply
and unit
, because it itself can be derived from apply
and unit
. 因此,不需要map
来从apply
和unit
定义map2
,因为它本身可以从apply
和unit
派生。
It behaves just like map
of any other Functor
(because every Applicative
is automatically a Functor
): given an F[A]
and an f: A => B
, it produces an F[B]
. 它的行为就像其他任何Functor
map
一样(因为每个Applicative
都自动是Functor
):给定F[A]
和f: A => B
,则产生F[B]
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.