函数式编程精神应用于命令式语言的现实生活例子?

发布于 2024-11-19 04:19:01 字数 384 浏览 6 评论 0原文

大多数人说,即使函数式编程也不太可能让你找到工作,但通过学习它,你可以成为更好的命令式/OO 程序员。

对我来说,主要是编写没有副作用的“非成员非朋友”函数。但我无法举出更多可以在命令式语言中有效应用函数式编程的例子,因为解决语言缺乏功能的问题通常太麻烦了。

那么,您在受函数式编程启发的非函数式语言中实际应用了哪些更多(具体)示例/技术?

我自己的另一个经历

这个是相当抽象的,但由于大多数 FP 语言缺乏“对象”,那里的文化倾向于严格的数据结构设计。通常,在 OOP 语言中,因为在类中填充额外的变量太容易,所以事情往往很快就会变得混乱。尽管使用 OCaml 和 Haskell 的记录语法也可以完成同样的工作,但这种方法在 FP 中感觉不合适。

Most people say that even functional programming is less likely to land you a job, you can become a better imperative/OO programmer by learning it.

For me, it's mostly about writing "non member non friend" functions that have no side effects. But I couldn't come up with more examples where functional programming can be effectively applied in imperative languages, because working around languages' lack of features is often too cumbersome.

So what are some more (specific) examples/techniques that you actually applied in non-functional languages that were inspired by functional programming?

Another of my own experience

This one is quite abstract, but due to the lack of "objects" in most FP languages, the culture there tends favor rigorous data structure design. Usually, in OOP languages, because stuffing an extra variable in a class is too easy, things tend to go mess up rather quickly. Though the same could be done using OCaml's and Haskell's record syntax, that kind of approach somehow feels out of place in FP.

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

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

发布评论

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

评论(4

蝶舞 2024-11-26 04:19:01

数据转换

根据我的经验,思考如何从功能上解决问题会让您更多地思考哪些数据转换为什么,而不是需要更改什么状态才能实现让这该死的东西继续运行......将

问题视为转换会使它们本身看起来不同 - 这会导致不同的且很可能更优雅的解决方案。

更新:在 c++ 中,有。标头,以及中的 std::transform

Data Transformation

In my experience thinking on how to solve a problem functionally makes you think more about what data gets transformed to what - and not what state needs to be changed in order to keep the damn thing running...

Thinking of problems as transformations makes them appear different all by itself - which leads to different and most likely more elegant solutions.

Update: In c++ there is the <functional> header, and std::transform in <algorithm>.

我不吻晚风 2024-11-26 04:19:01

大多数 Ruby 可枚举方法都受到函数式编程中的高阶函数的启发

Most Ruby Enumerable methods are inspired by Higher Order Functions from Functional programming

我偏爱纯白色 2024-11-26 04:19:01

新的 JavaScript 数组函数、filter、map、every、some、reduce 和 reduceRight 都是受函数式启发的。

The new-ish JavaScript array functions, filter, map, every, some, reduce, and reduceRight, are functional-inspired.

慢慢从新开始 2024-11-26 04:19:01

评论中已经提到了功能性 Java,但是 Apache Commons Collections 中也有一些功能性的东西。请参阅 org.apache.commons.collections.functors 包。

Functional Java was already mentioned in the comments, but there is also some functional-ish stuff in Apache Commons Collections. See the org.apache.commons.collections.functors package.

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