应用风格的实际用途是什么? [英] What are practical uses of applicative style?

查看:101
本文介绍了应用风格的实际用途是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是一名Scala程序员,现在正在学习Haskell。很容易找到面向对象概念的实际用例和真实世界的例子,例如装饰器,策略模式等。书籍和网站都充斥着它。

我意识到这在某种程度上不适用于功能概念。例如:应用程式



我正在努力寻找应用程序的实际使用案例。几乎到目前为止我所遇到的所有教程和书籍都提供了 [] Maybe 的示例。我希望应用程序比这更适用,看到它们在FP社区的所有关注。



我认为我理解了 applicatives 的概念基础(也许我错了),而且我在启蒙时刻等了很久。但似乎并没有发生。从不编程时,我有没有一刻我会高兴地大喊:尤里卡!我可以在这里使用应用程序! (除此之外,对于 [] 也许)。

有人能指导我如何在日常编程中使用应用程序吗?我如何开始发现这种模式?感谢!

解决方案

警告:我的回答颇为道奇/歉意。那么,起诉我。



那么,您在日常Haskell编程中多久创建一个新的数据类型?听起来你想知道什么时候创建你自己的Applicative实例,除非你在自己的解析器上滚动,否则你可能不需要做太多的事情。另一方面,使用应用程序实例,您应该学会频繁地进行操作。

Applicative不是像装饰器或策略那样的设计模式 。这是一种抽象,它使得它更普遍,更普遍有用,但更不明显。你很难找到实际用途的原因是因为这个例子使用它太简单了。你使用装饰器在窗户上放置滚动条。您可以使用策略统一您的国际象棋机器人的攻防动作界面。但是,适用于什么?那么,它们就更加普遍化了,所以很难说出它们的用途,没关系。应用程序在分析组合器时非常方便; Yesod网络框架使用Applicative来帮助建立和提取表单中的信息。如果你看,你会发现一百万和一个使用Applicative;这是全部的地方。但是因为它太抽象了,所以你只需要感受它就能识别出许多可以让你的生活更轻松的地方。


I am a Scala programmer, learning Haskell now. It's easy to find practical use cases and real world examples for OO concepts, such as decorators, strategy pattern etc. Books and interwebs are filled with it.

I came to the realization that this somehow is not the case for functional concepts. Case in point: applicatives.

I am struggling to find practical use cases for applicatives. Almost all of the tutorials and books I have come across so far provide the examples of [] and Maybe. I expected applicatives to be more applicable than that, seeing all the attention they get in the FP community.

I think I understand the conceptual basis for applicatives (maybe I am wrong), and I have waited long for my moment of enlightenment. But it doesn't seem to be happening. Never while programming, have I had a moment when I would shout with a joy, "Eureka! I can use applicative here!" (except again, for [] and Maybe).

Can someone please guide me how applicatives can be used in a day-to-day programming? How do I start spotting the pattern? Thanks!

解决方案

Warning: my answer is rather preachy/apologetic. So sue me.

Well, how often in your day-to-day Haskell programming do you create new data types? Sounds like you want to know when to make your own Applicative instance, and in all honesty unless you are rolling your own parser, you probably won't need to do it very much. Using applicative instances, on the other hand, you should learn to do frequently.

Applicative is not a "design pattern" like decorators or strategies. It is an abstraction, which makes it much more pervasive and generally useful, but much less tangible. The reason you have a hard time finding "practical uses" is because the example uses for it are almost too simple. You use decorators to put scrollbars on windows. You use strategies to unify the interface for both aggressive and defensive moves for your chess bot. But what are applicatives for? Well, they're a lot more generalized, so it's hard to say what they are for, and that's OK. Applicatives are handy as parsing combinators; the Yesod web framework uses Applicative to help set up and extract information from forms. If you look, you'll find a million and one uses for Applicative; it's all over the place. But since it's so abstract, you just need to get the feel for it in order to recognize the many places where it can help make your life easier.

这篇关于应用风格的实际用途是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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