Java 中的函数式编程

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1267297/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 07:37:16  来源:igfitidea点击:

Functional Programming in Java

javafunctional-programming

提问by ripper234

Is there a good library for functional programming in Java?

有没有一个很好的 Java 函数式编程库?

I'm looking for stuff like Predicateand List.Find()(as a static method). Not complicated to implement, but it would be nice to find a reusable library here.

我正在寻找PredicateList.Find() 之类的东西(作为静态方法)。实现起来并不复杂,但在这里找到一个可重用的库会很好。

采纳答案by dfa

FunctionalJavais the best known library; it makes use of Java closures (BGGA) for examples:

FunctionalJava是最著名的库;它使用 Java 闭包 ( BGGA) 作为示例:

final Array<Integer> a = array(1, 2, 3);  
final Array<Integer> b = a.map({int i => i + 42});  
arrayShow(intShow).println(b); // {43,44,45}  

EDIT

编辑

Check also lambdaj.

还要检查lambdaj

Further EDIT

进一步编辑

BGGA is entirely optional. It just makes for nicer syntax.

BGGA 是完全可选的。它只是为了更好的语法。

回答by mikej

Functional Javais one that's worth taking a look at and FunctionalJis another.

Functional Java是值得一看的,而FunctionalJ是另一个。

回答by Chi

Google collectionshas a decent selection of functional-programming style utility methods. Some classes of interest are Iterables, Iterators, Function, Functions, etc

谷歌集合有一个不错的函数式编程风格的实用方法选择。一些感兴趣的类是 Iterables、Iterators、Function、Functions 等

It also has a bunch of collection classes as well!

它也有一堆收集类!

回答by Gordon Gustafson

Scalais a functional programming language that is fully compatible with Java (runs through the JVM). It offers a beautiful mix of object-oriented and functional techniques along with many improvements over Java in generics and concurrency. Some even say it could replace Java.

Scala是一种与 Java 完全兼容的函数式编程语言(通过 JVM 运行)。它提供了面向对象和函数式技术的完美结合,以及在泛型和并发方面对 Java 的许多改进。有人甚至说它可以取代Java。

回答by Johan Kullbom

Jambdais another FP-library. From the documentation:

Jambda是另一个 FP 库。从文档

Jambda is an attempt to provide the Java(TM) world with tools and concepts from functional programming (FP).

The goals are several:

  • To provide Java programmers with expressive FP constructs
  • To provide a bridge for Java programmers into the FP-world
  • To see how far Java and generics can be stretched

This document is an attempt to introduce Java programmers into the FP world, and at the same time explain some (or most) of the features in Jambda.

Jambda 试图为 Java(TM) 世界提供来自函数式编程 (FP) 的工具和概念。

目标有几个:

  • 为 Java 程序员提供富有表现力的 FP 结构
  • 为 Java 程序员提供进入 FP 世界的桥梁
  • 看看 Java 和泛型可以延伸多远

本文档试图将 Java 程序员引入 FP 世界,同时解释 Jambda 中的部分(或大部分)功能。

回答by Mario Fusco

If you want a pure Java solution check out lambdaj

如果您想要纯 Java 解决方案,请查看 lambdaj

http://code.google.com/p/lambdaj/

http://code.google.com/p/lambdaj/

Besides the possibility to define and use closure in a DSL-style, it also allows to manipulate collections in a functional way, without explicitly write closures or loops

除了可以以 DSL 样式定义和使用闭包之外,它还允许以函数方式操作集合,而无需显式编写闭包或循环

回答by MatrixFrog

Apache Commons has some functional-ish code in it. See for example, the Predicateinterface.

Apache Commons 中有一些功能性代码。例如,参见Predicate接口。

回答by thSoft

Google Guavahas functional:

谷歌番石榴具有以下功能:

  • collection operations
  • concurrency constructs (Futures)
  • 收集操作
  • 并发构造(期货)

回答by haylem

Java Libraries

Java 库

There are libraries that can help you do this, by already doing the legwork for you and hiding the arcane things:

有一些库可以帮助您做到这一点,它们已经为您完成了跑腿工作并隐藏了神秘的东西:

Mature / Established Libraries

成熟/已建立的图书馆

More Obscure / Experimental Libraries

更多晦涩/实验图书馆

These will allow you to write Java code with a more functional approach and possibly more familiar syntax and semantic, as you'd expect from an FP-competent language. Within reason, that is.

这些将允许您使用功能更强大的方法和可能更熟悉的语法和语义来编写 Java 代码,正如您对具有 FP 能力的语言所期望的那样。在合理范围内,那就是。

JVM Languages

JVM 语言

And obviously, you can implementa functional language on top of Java. So that you can then use that one as your FP language. Which is a bit of a higher-level of abstraction than what you asked for, but relatively within context (though I'm cheating a bit here, granted).

显然,您可以在 Java 之上实现函数式语言。这样您就可以将其用作您的 FP 语言。这比您要求的抽象级别更高,但相对而言在上下文中(尽管我在这里作弊,这是理所当然的)。

For instance, check out:

例如,查看:

Quite Mature Languages

相当成熟的语言

Less Mature or More Obscure Languages

不太成熟或更晦涩的语言

Further Reading

进一步阅读

You may also want to read or watch these articles or videos:

您可能还想阅读或观看这些文章或视频:



Taken from my P.SEanswer to "Is Functional Programming Possible in Java?"

摘自我对Java 中的函数式编程是否可行?”的P.SE回答

回答by Johan Norén

Or download OpenJDK 8 to try out Lambda expressions the way they will become in Java 8. Among others, the collection APIs are adjusted to support a functional style. See http://macgyverdev.blogspot.se/2012/10/functional-programming-in-java.htmlfor examples of new collection APIs and comparisons with Guava, LambdaJ and FunctionalJava.

或者下载 OpenJDK 8 以尝试使用 Java 8 中的 Lambda 表达式。其中,集合 API 进行了调整以支持函数式风格。有关 新集合 API 的示例以及与 Guava、LambdaJ 和 FunctionalJava 的比较,请参阅 http://macgyverdev.blogspot.se/2012/10/functional-programming-in-java.html