在 Scala 类型参数中,做什么 ->>和->>>吝啬的? [英] In Scala type parameters, what do ->> and ->>> mean?
问题描述
Heiko Seeberger 在此处写了一篇关于范畴论的精彩博文:
Heiko Seeberger wrote a great blog post on category theory here:
https://hseeberger.wordpress.com/2010/11/25/introduction-to-category-theory-in-scala/
在其中,他像这样定义了一个 GenericFunctor:
In it, he defines a GenericFunctor like so:
trait GenericFunctor[->>[_, _], ->>>[_, _], F[_]] {
def fmap[A, B](f: A ->> B): F[A] ->>> F[B]
}
我没有找到对文档中 ->> 和 ->>> 符号的文档引用.有人可以解释一下他们在做什么吗?
I did not have any luck finding documentation references to the ->> and ->>> symbols in documentation. Could someone please explain what they are doing?
推荐答案
符号本身没有任何意义.他们是 Heiko 随意取的名字:
The symbols themselves don't mean anything. They are arbitrary names Heiko picked:
> class Foo[A, B]
defined class Foo
> class Foo[M1[_], M2[_]]
defined class Foo
> class GenericFunctor[->>[_, _], ->>>[_, _], F[_]]
defined class GenericFunctor
它们是类型参数的一部分,它们本身就是类型构造函数(如果您想听起来很花哨,可以使用更高级的类型).类型应用程序可以写中缀,所以 A ->>B
与 ->>[A, B]
相同.
They are parts of type parameters that they themselves are type constructors (higher-kinded types if you want to sound fancy).
Type applications can be written infix, so A ->> B
is same as ->>[A, B]
.
根据正在发生的事情......海科说
As per what's going on... Heiko says
查看成分,我们找到了我们需要的一切:类型 A
和 B
映射到类型 F[A]
和 F[B]
并映射 A ->>B
映射到映射 F[A] ->>>>F[B]
.
Looking at the ingredients, we find all that we need: Types
A
andB
are mapped to typesF[A]
andF[B]
and mapsA ->> B
are mapped to mapsF[A] ->>> F[B]
.
既然我们在谈论范畴论,我们想避免使用术语函数,因为它是特定于实现的,但我们想描述一些类似于函数的东西.在他们的行话中,类似功能的东西是箭头.我们需要其中的两个,因为我们不想假设传入和传出的箭头相同.这两个箭头由->>
和->>>>
表示.F[_]
是一个类似于 List
和 Option
的容器.我觉得..
Since we are talking category theory, we want to avoid the term function because that's implementation specific, but we want to describe something kind of like a function. Something-like-a-function in their lingo is an arrow. We need two of them since we don't want to assume the incoming and outgoing arrows to be the same. These two arrows are represented by ->>
and ->>>
. F[_]
is a container like List
and Option
. I think..
所以 fmap
(在 Scala 中又名 map
方法)接受一个值的箭头并返回另一个容器的箭头.除了与 map
方法不同,fmap
返回一个带有容器的箭头.
So fmap
(aka map
method in Scala) takes an arrow of values and returns another arrow of containers. Except unlike map
method, fmap
returns an arrow that takes a container.
使用 Function
两个箭头的 GenericFunctor
的特定应用是 Functor
.而容器使用List
的Functor
的具体应用是ListFunctor
.
A specific application of the GenericFunctor
using Function
for both arrows is Functor
. And specific application of Functor
that uses List
for the container is ListFunctor
.
object ListFunctor extends Functor[List] {
def fmap[A, B](f: A => B): List[A] => List[B] = as => as map f
}
所以这是将一个函数从 A
取到 B
,然后将一个函数从 List[A]
返回到 List[B]
,内部调用map
.
So that's taking a function from A
to B
, and returning a function from List[A]
to List[B]
, calling map
internally.
这篇关于在 Scala 类型参数中,做什么 ->>和->>>吝啬的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!