如C#语言的是否单子IO有意义 [英] Does an IO monad make sense in a language like C#

查看:122
本文介绍了如C#语言的是否单子IO有意义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

花了很多时间阅读和思考之后,我觉得我终于掌握是什么单子,它们是如何工作的,什么他们是很有用的。我的主要目标是要弄清楚,如果单子是什么,我可以适用于我在C#的日常工作​​。

After spending a lot of time reading and thinking, I think I have finally grasped what monads are, how they work, and what they're useful for. My main goal was to figure out if monads were something I could apply to my daily work in C#.

当我开始学习单子,我得到的印象是,他们是神奇,他们以某种方式使IO以及其它非纯函数纯洁。

When I started learning about monads, I got the impression that they are magical, and that they somehow make IO and other non-pure functions pure.

我明白的单子在净像LINQ的东西的重要性,也许是非常有用的处理功能不返回有效值。而且我也很欣赏需要限制有状态代码和隔离外部依赖,而我希望的单子将与那些过于帮助。

I understand the importance of monads for things like LINQ in .Net, and Maybe is very useful for dealing with functions that don't return valid values. And I also appreciate the need to limit statefulness in code and to isolate external dependencies, and I was hoping monads would help with those too.

不过,我终于体会到得出结论认为,IO和处理状态单子是Haskell中的必需品,因为哈斯克尔没有其他办法做到这一点(否则,你不能保证测序,有的电话会被优化掉。)但是更多的主流语言,单子是不是非常适合这些需求,因为大多数语言已经处理以及州和IO容易。

But I've finally come to the conclusion that monads for IO and handling state are a necessity for Haskell, because Haskell has no other way to do it (otherwise, you couldn't guarantee sequencing, and some calls would be optimized away.) But for more mainstream languages, monads are not a good fit for these needs, since most languages already handle and state and IO easily.

所以,我的问题是,它是公平地说,在IO单子真的是唯一有用的在Haskell?有没有办法实现,比如C#的IO单子一个很好的理由?

So, my question is, is it fair to say that the IO monad is really only useful in Haskell? Is there a good reason to implement an IO monad in, say, C#?

推荐答案

我经常使用Haskell和F#和我从来没有真的觉得使用F#的IO或状态单子。

I use Haskell and F# regularly and I've never really felt like using an IO or state monad in F#.

对我来说,最主要的原因是,在Haskell中,你可以从事物的类型,它告诉的使用IO或状态,这是信息的真正有价值的作品。

The main reason for me is that in Haskell, you can tell from the type of something that it doesn't use IO or state, and that's a really valuable piece of information.

在F#(和C#)有上没有这样的普遍期待别人的代码,这样你就不会从补充说,纪律到你自己的代码更受益,你会付出坚持它的一些普通开销(主要是语法)。

In F# (and C#) there's no such general expectation on other people's code, and so you won't benefit much from adding that discipline to your own code, and you'll pay some general overhead (mainly syntactic) for sticking to it.

单子也没有在.NET平台上的工作也很好,因为缺乏-kinded更高类型的:虽然您可以在F#工作流的语法写单子的代码,在C#中多一点痛苦,你不能轻易地写在抽象多种不同的单子代码

Monads also don't work too well on the .NET platform because of the lack of higher-kinded types: while you can write monadic code in F# with workflow syntax, and in C# with a bit more pain, you can't easily write code that abstracts over multiple different monads.

这篇关于如C#语言的是否单子IO有意义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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