简体   繁体   English

类别理论中的应用程序中的地图功能有什么作用?

[英]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 ,只使用applyunit

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 ,从而获得仅依赖applyunitmap2定义:

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来从applyunit定义map2 ,因为它本身可以从applyunit派生。

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM