简体   繁体   English

在C#中使用Monads的令人信服的场景是什么?

[英]What is the compelling scenario for using Monads in C#

Let me state up front that I have an infantile understanding of Monads. 让我先说明我对Monads有一种幼稚的理解。 I have read the various threads on Monads here and have done a few hours of study on the concept. 我已经阅读了Monads上的各种主题,并对这个概念进行了几个小时的研究。 I hardly feel comfortable with the term, but I think it is safe to say that I generally understand what a Monad is/does. 我对这个词几乎感到不舒服,但我认为可以说我通常理解Monad是什么/做什么。

I'm a C# developer who is looking to improve the way I work. 我是一名C#开发人员,希望改进我的工作方式。 What would help me further in my Monaducation is see a real world application of a Monad in C# (ie via a linq SelectMany() or somesuch) that is clearly an improvement over other ways of solving the same sort of problem in oldskool C#. 在我的Monaducation中进一步帮助我的是在C#中看到Monad的真实世界应用(即通过linq SelectMany(或其他),这显然比在oldskool C#中解决同类问题的其他方法有所改进。

Has anyone seen such a beast? 有没有人见过这样的野兽?

Here is one such scenario: you want to author a parsing library (a nice example of an embedded DSL), and you discover that the best ones are monadic parser combinator libraries. 下面是一个这样的场景:你想创作一个解析库(嵌入式DSL的一个很好的例子),你发现最好的是monadic解析器组合库。 So you write it leveraging LINQ syntax sugars to author C# code that has the same structure as the grammar of the language you're parsing, and you get the benefits of an awesome programming model for on-the-fly semantic analysis and error-recovery. 因此,您可以利用LINQ语法糖来编写C#代码,这些代码具有与您正在解析的语言语法相同的结构,并且您可以获得实时语义分析和错误恢复的强大编程模型的好处。 See this blog for a description. 有关说明,请参阅此博客

Find Pythagorean triples: 找到毕达哥拉斯三重奏:

  var r = from a in Enumerable.Range(1, 25)
          from b in Enumerable.Range(a, 25-a)
          from c in Enumerable.Range(b, 25-b)
          where a*a + b*b == c*c
          select new [] { a, b, c };

Here is one such scenario: you want to write code that makes sequential async calls (eg IO) without holding threads, but you don't want to write the hopeless tangle of spaghetti that the async programming model (BeginFoo/EndFoo) forces you into. 下面是一个这样的场景:你想要编写代码来进行顺序异步调用(例如IO),而不需要保存线程,但是你不想编写异步编程模型(BeginFoo / EndFoo)强迫你进入的意大利面条。 。 So you can use a monad and LINQ sugars and write code that looks straight-line but it releases/switches threads throughout. 因此,您可以使用monad和LINQ糖并编写看起来直线的代码,但它会在整个过程中释放/切换线程。 See this blog for a short description. 有关简短说明,请参阅此博客

一个例子是使用Maybe monad简化null检查,如本文所示。

Programming with monads is declarative, describing what you want at a high level rather than the low-level details of how to generate it. 使用monad进行编程是声明性的,在高级别描述您想要的内容,而不是如何生成它的低级细节。

See the exercises at the end of Brian Beckman's state-monad talk on Channel 9 . 请参阅布莱恩贝克曼在第9频道的州议员谈话 结束时的练习

I recently blogged about refactoring a typical imperative real-world C# code (a function in NuGet ) to functional, monadic style (more concretely, using the Maybe monad ). 我最近写了一篇关于将典型命令式真实世界C#代码( NuGet中的函数)重构为函数式monadic样式的博客 (更具体地说,使用Maybe monad )。 I did my best to do it in little steps, explaining the rational behind step, so I think it helps in understanding how monads are useful. 我尽我所能在很短的步骤中完成它,解释合理的步骤,所以我认为它有助于理解monad是如何有用的。

LINQ is used in many solutions (and often requested in questions) here on StackOverflow. LINQ在StackOverflow上的许多解决方案(通常在问题中请求)中使用。 Review questions with the LINQ tag and you will see real world usage. 使用LINQ标签查看问题,您将看到实际使用情况。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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