monad 除了提高可读性和生产力之外还有其他作用吗?

发布于 2024-12-23 05:01:16 字数 286 浏览 2 评论 0原文

在过去的几个月里,我一直在研究 monad(函子和应用函子)。我一直试图弄清楚单子在一般意义上何时有用。如果我正在查看一段代码,我会问,我应该使用特定的 monad 还是通过变压器使用堆栈?在我的努力中,我认为我已经找到了答案,但我希望其他人提供意见,以防我错过了一些东西。在我看来,单子对于抽象出特定的管道以提高一段代码的可读性/声明性非常有用,这可能会通过需要编写更少的代码来提高生产力。我能找到的唯一例外是 IO monad,它试图在 IO 面前保持纯函数的纯粹性。似乎给定的 monad 无法提供通过其他方式无法实现的问题的解决方案。我错过了什么吗?

I have been looking at monads a lot over the past few months (functors and applicative functors as well). I have been attempting to figure out when monads are useful in a general sense. If I am looking at a piece of code I ask, should I employ a specific monad or a stack via transformers? In my efforts I think I have found an answer but I want others input in case I have missed something. It appears to me that monads are useful for abstracting away specific plumbing to increase readabilty/the declaritive nature of a piece of code which can have a side affect of increasing productivity by requiring less code to write. The only exception I can find is the IO monad which attempts to keep a pure function pure in the face of IO. It doesn't appear that a given monad provides a solution to a problem that can't be acheived via other means. Am I missing something?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

热风软妹 2024-12-30 05:01:16

除了图灵完备性之外,是否有任何功能可以提供通过其他方式无法实现的问题的解决方案?没有。所有图灵等效语言都是表达相同基本事物的不同方式。由于 monad 是由更基本的构建块构建的,因此显然这组构建块能够完成 monad 可以做的任何事情。当我们谈论一种语言或功能“允许”我们做某事时,我们的意思是它允许我们以一种易于理解的方式自然地表达该事情。

Does any feature beyond mere Turing-completeness provide a solution to a problem that can't be achieved via other means? Nope. All Turing-equivalent languages are different ways of expressing the same basic things. Since monads are built out of more fundamental building blocks, obviously that set of building blocks is able to do anything a monad can. When we talk about a language or feature "allowing" us to do something, we mean it allows us to express that thing naturally, in a way that's easy to understand.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文