typescript中函数_TypeScript中的Monoid

typescript中函数

“Alternatively, the fundamental notion of category theory is that of a Monoid”

“或者,范畴论的基本概念是单义词的概念”

— Categories for the Working Mathematician

—工作数学家的类别

Monoids are one of those profound ideas that are easy to understand and are everywhere. Monoids belong to the category of Algebras. There are a couple of very important ways to organize different structures that appear in functional programming. Algebras is one of them. Fantasy land has some of the most important algebras in its specification.

Monoid是易于理解且无处不在的深刻思想之一。 Monoid属于代数的范畴。 有两种非常重要的方法来组织功​​能编程中出现的不同结构。 代数就是其中之一。 幻想世界在其规格中具有一些最重要的代数。

Wikipedia says a monoid is an algebraic structure with a single associative binary operation and an identity element. Any structure that has those two elements is a monoid.

维基百科说,类四面体是具有单个关联 二进制运算和一个标识元素代数结构 。 具有这两个元素的任何结构都是一个单面体。

Suppose that S is a structure and ⊗ is some binary operation S ⊗ S → S, then S with ⊗ is a monoid if it satisfies the following two axioms:

假设S是一个结构,并且⊗是某个二进制运算 S ⊗ S → S ,则满足two的S是单等式,如果它满足以下两个公理:

1. Associativity: For all a, b and c in S, the equation (a ⊗ b) ⊗ c = a ⊗ (b ⊗ c) holds.

1. 关联性 :对于S中的所有a,b和c,方程(a ⊗ b) ⊗ c = a ⊗ (b ⊗ c)成立。

2. Identity element: There exists an element e in S such that for every element a in S, the equations e ⊗ a = a ⊗ e = a hold.

2. 身份元素 :S中存在一个元素e,因此对于S中的每个元素a,等式e ⊗ a = a ⊗ e = a保持。

Let us take the integers we can take addition as a binary operation between two integers. The addition is associative. This means there is no importance on the order of the addition.

让我们采用整数,我们可以将加法运算作为两个整数之间的二进制运算。 加法是关联的 。 这意味着添加顺序并不重要。

(x+(y+z)) = ((x+y) +z)

(x+(y+z)) = ((x+y) +z)

And 0 is the identity element for addition since :

并且0是要添加的标识元素,因为:

x + 0 = x

x + 0 = x

For those reasons, the pair (+,0) forms a monoid over the integers. However, we can form different other monoids over the integers. For example, multiplication * also can be viewed as a monoid with its respective identity element the 1. In this way the pair (*,1) is another monoid over the integers. Strings in TS have a default monoid, which is formed by the concatenation and the empty string (‘’, concat). String type in TS has natively a concat method that refers to this specific monoid we all know. However, for integers, for example, it would not make any sense to pick a specific monoid as default.

由于这些原因,该对(+,0)在整数上形成一个单面体。 但是,我们可以在整数上形成其他不同的等分体。 例如,乘法*也可以看作是具有其各自的标识元素1的单面体。 通过这种方式,对(*,1)是整数上的另一个monoid。 TS中的字符串具有默认的monoid,它由串联和空字符串('', concat) 。 TS中的字符串类型本身具有concat方法,该方法引用了我们都知道的此特定monoid。 但是,例如对于整数,选择特定的monoid作为默认值没有任何意义。

TypeScript公式 (TypeScript Formulation)

We will use monoids in our code by defining simple object literals that have two functions, one that returns the identity element called empty and one that represents the binary operation called concat .

我们将通过定义具有两个函数的简单对象常量来在代码中使用monoid,其中一个函数返回两个称为的标识元素,另一个函数表示一个称为concat的二进制操作。

We can define for example:

我们可以定义例如:

const Sum: monoid<number> = ({
empty: 0,
concat: (u: number, v: number) => u + v
})

which we will use it like this:

我们将像这样使用它:

var total:number = Sum.concat(Sum.empty, Sum.concat(3, 4));

var total:number = Sum.concat(Sum.empty, Sum.concat(3, 4));

This definition would allow us to use it with the reduce function of a list in a direct manner

此定义将使我们可以直接将其与列表的reduce函数一起使用

total = [ 1, 3, 4, 5 ].reduce( sum.concat ,sum.empty);

total = [ 1, 3, 4, 5 ].reduce( sum.concat ,sum.empty);

替代定义 (Alternative Definition)

Alternatively, if we want to have closure [meaning that the return type of the concat and empty is of the same type with the initial object], we could define one new class for each monoid.

另外,如果我们要进行闭包 [意味着concat和empty的返回类型与初始对象具有相同的类型],则可以为每个monoid定义一个新类。

Run This: Fiddle

运行此: 小提琴

This formulation is nice in the sense that allows for fluent chaining var total = new SumAcc (0).concat(new SumAcc (1)) .concat(new SumAcc (2)) because of the fact that empty and concat both return a result of the same type. This also is more congruent with the mathematical definition that says that if S is a structure (in a programming language, this translates to a Type), then S × S → S, means that the operation returns a structure of the same kind.

这种表示法在允许流畅链接 var total = new SumAcc (0).concat(new SumAcc (1)) .concat(new SumAcc (2))的意义上是好的,因为empty和concat都返回结果相同类型的。 这也更符合数学定义,即S是结构(在编程语言中,它转换为Type),则S × S → S意味着操作返回相同类型的结构。

折叠半身像 (Folding monoids)

Monoids have this very desirable property that if we keep applying this binary operation, we can always reduce the computation to a single element of the same type:

Monoid具有非常理想的属性,如果我们继续应用此二进制运算,我们总是可以将计算减少为相同类型的单个元素:

(M⊗…(M (⊗(M ⊗ M))) → M

(M⊗…(M (⊗(M ⊗ M))) → M

this is called folding. We already know folding as reduce from the TS Array type. However, we are going to generalize fold in a latter chapter for data structures that we will call traversables. Coming back to fold, let us try to derive reduce.

这称为折叠。 我们已经从TS Array类型中知道折叠为减少 。 但是,我们将在后面的章节中将可折叠性归纳为可遍历的数据结构。 回到折叠,让我们尝试得出减少量。

Let’s say we have some sequence of integers 2,4,5,6 and we want to add them this a beginner level problem:

假设我们有一些整数2,4,5,6的序列,我们想将它们添加为初学者级别的问题:

var list =  [ 1, 2, 3, 4 ];
var total = 0; foreach (var item in list)
total = total + item;

I highlighted there the elements of a monoid. So, let’s abstract away. If we replace the (0,+) with any other monoid this should work, so why not abstract and reuse.

我在那里强调了一个半身像的元素。 因此,让我们抽象一下。 如果我们将(0,+)替换为其他任何等分体,则此方法应该起作用,所以为什么不提取和重用。

Run This: Fiddle

运行此: 小提琴

Now we can use this function with all kind of monoids

现在,我们可以将此函数与所有类半定式一起使用

Run This: Fiddle

运行此: 小提琴

Next:

下一个:

翻译自: https://medium.com/@dimpapadim3/monoids-in-typescript-59a9c1510993

typescript中函数

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值