帮助C#开发人员了解:什么是单子? [英] Help a C# developer understand: What is a monad?

查看:241
本文介绍了帮助C#开发人员了解:什么是单子?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有很多谈单子的这些日子。我读了几篇文章/博客文章,但我不能用自己的例子远远不够完全掌握的概念。其原因是单子是一个功能性的语言概念,从而例子在我还没有使用过的语言(因为我没有使用过深入函数式语言)。我不能掌握语法更深,充分遵循文章...但我可以告诉有东西值得理解那里。

There is a lot of talk about monads these days. I have read a few articles / blog posts, but I can't go far enough with their examples to fully grasp the concept. The reason is that monads are a functional language concept, and thus the examples are in languages I haven't worked with (since I haven't used a functional language in depth). I can't grasp the syntax deeply enough to follow the articles fully ... but I can tell there's something worth understanding there.

但是,我知道C#pretty以及包括拉姆达前pressions等功能特性。我知道C#仅有功能功能的子集,所以也许单子不能在C#pssed前$ P $。

However, I know C# pretty well, including lambda expressions and other functional features. I know C# only has a subset of functional features, and so maybe monads can't be expressed in C#.

不过,肯定是可以传达的概念?至少我希望如此。也许你可以present一个C#示例作为基础,然后描述什么是C#开发人员的希望,他可以从那里做的,但不能因为缺乏语言功能的编程功能。这将是美妙的,因为它会传达的意图和单子的好处。因此,这里是我的问题:什么是你可以到C#3开发商给的单子最好的解释

However, surely it is possible to convey the concept? At least I hope so. Maybe you can present a C# example as a foundation, and then describe what a C# developer would wish he could do from there but can't because the language lacks functional programming features. This would be fantastic, because it would convey the intent and benefits of monads. So here's my question: What is the best explanation you can give of monads to a C# 3 developer?

谢谢!

(编辑:顺便说一句,我知道至少有3什么是单子的问题已经在SO不过,我面临着同样的问题,他们......所以IMO需要这个问题,因为。 C#-developer关注,谢谢。)

( By the way, I know there are at least 3 "what is a monad" questions already on SO. However, I face the same problem with them ... so this question is needed imo, because of the C#-developer focus. Thanks.)

推荐答案

你的大多数编程整天做的是一些功能相结合,从他们建立更大的功能。通常你有你的工具箱中,不仅功能,还包括其他的东西像运营商,变量赋值之类的,但一般你的程序很多计算的结合在​​一起,将被组合在一起更大的计算进一步。

Most of what you do in programming all day is combining some functions together to build bigger functions from them. Usually you have not only functions in your toolbox but also other things like operators, variable assignments and the like, but generally your program combines together lots of "computations" to bigger computations that will be combined together further.

一个单子一些方法来做到这一点计算的结合。

A monad is some way to do this "combining of computations".

通常你的最基本的经营者两个计算结合起来是;

Usually your most basic "operator" to combine two computations together is ;:

a; b

当你说这你的意思是先做 A ,然后执行 B 。结果 A; b 基本上是再次证明可以与更多的东西结合起来进行计算。
这是一个简单的单子,这是小的计算精梳到更大的人的方法。在; 说:做在左边的事情,然后做右侧的事

When you say this you mean "first do a, then do b". The result a; b is basically again a computation that can be combined together with more stuff. This is a simple monad, it is a way of combing small computations to bigger ones. The ; says "do the thing on the left, then do the thing on the right".

这可以看作是在面向对象语言中的单子另一件事是。通常情况下,你觉得这样的事情:

Another thing that can be seen as a monad in object oriented languages is the .. Often you find things like this:

a.b().c().d()

基本上意味着评估左侧的计算,然后调用上的,其结果右侧的方法。这是另一种方式的功能/计算相结合,有一点比更复杂; 。并与链接的东西放在一起的概念是一个单子,因为它是两次计算相结合,以一种新的计算方式。

The . basically means "evaluate the computation on the left, and then call the method on the right on the result of that". It is another way to combine functions/computations together, a little more complicated than ;. And the concept of chaining things together with . is a monad, since it's a way of combining two computations together to a new computation.

另一个相当常见的单子,也没有特殊的语法,是这个模式:

Another fairly common monad, that has no special syntax, is this pattern:

rv = socket.bind(address, port);
if (rv == -1)
  return -1;

rv = socket.connect(...);
if (rv == -1)
  return -1;

rv = socket.send(...);
if (rv == -1)
  return -1;

的返回值-1表示失败,但有抽象出此错误检查没有真正的方法,即使你有大量的API的调用,你需要以这种方式结合起来。这基本上只是一个单子,通过规则结合函数调用如果左边的函数返回-1,做回自己-1,否则调用右边的功能。如果我们有一个运营商>> = 的做这件事情,我们可以简单的写:

A return value of -1 indicates failure, but there is no real way to abstract out this error checking, even if you have lots of API-calls that you need to combine in this fashion. This is basically just another monad that combines the function calls by the rule "if the function on the left returned -1, do return -1 ourselves, otherwise call the function on the right". If we had an operator >>= that did this thing we could simply write:

socket.bind(...) >>= socket.connect(...) >>= socket.send(...)

这会使事情更易读,并有助于抽象出我们的组合功能的特殊方式,这样我们就不需要一遍遍重复自己。

It would make things more readable and help to abstract out our special way of combining functions, so that we don't need to repeat ourselves over and over again.

和有更多的方式来功能/计算是作为一种普遍现象有用的,可以在一个单子被抽象相结合,使单子的用户编写更加简洁明了code,因为所有的的使用的功能簿记和管理的单子做。

And there are many more ways to combine functions/computations that are useful as a general pattern and can be abstracted in a monad, enabling the user of the monad to write much more concise and clear code, since all the book-keeping and management of the used functions is done in the monad.

例如上面的>> = 可扩大到做错误检查,然后调用,我们得到了作为输入插座在右侧因此,我们并不需要明确指定插座很多次了:

For example the above >>= could be extended to "do the error checking and then call the right side on the socket that we got as input", so that we don't need to explicitly specify socket lots of times:

new socket() >>= bind(...) >>= connect(...) >>= send(...);

的正式定义是一个比较复杂一点,因为你不必担心如何为输入到下一个得到一个函数的结果,如果该功能需要与输入,因为你要确保该功能你结合到适合您尝试他们在您的单子相结合的方式。但基本的概念就是,你正式不同的方式来组合功能结合在一起。

The formal definition is a bit more complicated since you have to worry about how to get the result of one function as an input to the next one, if that function needs that input and since you want to make sure that the functions you combine fit into the way you try to combine them in your monad. But the basic concept is just that you formalize different ways to combine functions together.

这篇关于帮助C#开发人员了解:什么是单子?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆