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

时间:2022-09-01 21:02:19

Let me state up front that I have an infantile understanding of Monads. I have read the various threads on Monads here and have done a few hours of study on the concept. I hardly feel comfortable with the term, but I think it is safe to say that I generally understand what a Monad is/does.

让我先说明我对Monads有一种幼稚的理解。我已经阅读了Monads上的各种主题,并对这个概念进行了几个小时的研究。我对这个词几乎感到不舒服,但我认为可以说我通常理解Monad是什么/做什么。

I'm a C# developer who is looking to improve the way I work. What would help me further in my Monaducation is see a real world application of a Monad in C# (i.e. via a linq SelectMany() or somesuch) that is clearly an improvement over other ways of solving the same sort of problem in oldskool C#.

我是一名C#开发人员,希望改善我的工作方式。在我的Monaducation中进一步帮助我的是在C#中看到Monad的真实世界应用(即通过linq SelectMany(或其他),这显然比在oldskool C#中解决同类问题的其他方法有所改进。

Has anyone seen such a beast?

有没有人见过这样的野兽?

8 个解决方案

#1


11  

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. 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. See this blog for a description.

下面是一个这样的场景:你想创建一个解析库(嵌入式DSL的一个很好的例子),你发现最好的是monadic解析器组合库。因此,您可以利用LINQ语法糖来编写C#代码,这些代码与您正在解析的语言的语法具有相同的结构,并且您可以获得用于实时语义分析和错误恢复的强大编程模型的好处。有关说明,请参阅此博客。

#2


5  

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 };

#3


4  

Here is one such scenario: you want to write code that makes sequential async calls (e.g. 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. So you can use a monad and LINQ sugars and write code that looks straight-line but it releases/switches threads throughout. See this blog for a short description.

下面是一个这样的场景:你想要编写代码来进行顺序异步调用(例如IO),而不需要保存线程,但是你不想编写异步编程模型(BeginFoo / EndFoo)强迫你进入的意大利面条。 。因此,您可以使用monad和LINQ糖并编写看起来直线的代码,但它会在整个过程中释放/切换线程。有关简短说明,请参阅此博客。

#4


3  

check out http://memoirsofaprogrammer.blogspot.com

看看http://memoirsofaprogrammer.blogspot.com

#5


3  

One example is simplifying null checks using the Maybe monad as shown in this article.

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

#6


0  

LINQ is used in many solutions (and often requested in questions) here on *. Review questions with the LINQ tag and you will see real world usage.

LINQ在*上的许多解决方案(通常在问题中请求)中使用。使用LINQ标记查看问题,您将看到实际使用情况。

#7


0  

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.

请参阅Brian Beckman在第9频道的州议会演讲结束时的练习。

#8


0  

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). 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.

我最近写了一篇关于将典型命令式真实世界C#代码(NuGet中的函数)重构为功能性monadic风格的博客(更具体地说,使用Maybe monad)。我尽力在很短的步骤内完成它,解释合理的步骤,所以我认为它有助于理解monad是如何有用的。

#1


11  

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. 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. See this blog for a description.

下面是一个这样的场景:你想创建一个解析库(嵌入式DSL的一个很好的例子),你发现最好的是monadic解析器组合库。因此,您可以利用LINQ语法糖来编写C#代码,这些代码与您正在解析的语言的语法具有相同的结构,并且您可以获得用于实时语义分析和错误恢复的强大编程模型的好处。有关说明,请参阅此博客。

#2


5  

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 };

#3


4  

Here is one such scenario: you want to write code that makes sequential async calls (e.g. 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. So you can use a monad and LINQ sugars and write code that looks straight-line but it releases/switches threads throughout. See this blog for a short description.

下面是一个这样的场景:你想要编写代码来进行顺序异步调用(例如IO),而不需要保存线程,但是你不想编写异步编程模型(BeginFoo / EndFoo)强迫你进入的意大利面条。 。因此,您可以使用monad和LINQ糖并编写看起来直线的代码,但它会在整个过程中释放/切换线程。有关简短说明,请参阅此博客。

#4


3  

check out http://memoirsofaprogrammer.blogspot.com

看看http://memoirsofaprogrammer.blogspot.com

#5


3  

One example is simplifying null checks using the Maybe monad as shown in this article.

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

#6


0  

LINQ is used in many solutions (and often requested in questions) here on *. Review questions with the LINQ tag and you will see real world usage.

LINQ在*上的许多解决方案(通常在问题中请求)中使用。使用LINQ标记查看问题,您将看到实际使用情况。

#7


0  

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.

请参阅Brian Beckman在第9频道的州议会演讲结束时的练习。

#8


0  

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). 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.

我最近写了一篇关于将典型命令式真实世界C#代码(NuGet中的函数)重构为功能性monadic风格的博客(更具体地说,使用Maybe monad)。我尽力在很短的步骤内完成它,解释合理的步骤,所以我认为它有助于理解monad是如何有用的。