Functional Programming in Java [closed]
Is there a good library for functional programming in Java?
I'm looking for stuff like Predicate and List.Find() (as a static method). Not complicated to implement, but it would be nice to find a reusable library here.
Solution 1:
FunctionalJava is the best known library; it makes use of Java closures (BGGA) for examples:
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.
Further EDIT
BGGA is entirely optional. It just makes for nicer syntax.
Solution 2:
Scala is 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.
Solution 3:
Java Libraries
There are libraries that can help you do this, by already doing the legwork for you and hiding the arcane things:
Mature / Established Libraries
- Functional Java
- Google guava
- LambdaJ
More Obscure / Experimental Libraries
- Fun4J
- JCurry
- OCaml-Java
- Jambda
- Bolts
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.
JVM Languages
And obviously, you can implement a 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).
For instance, check out:
Quite Mature Languages
- Clojure
- Scala
Less Mature or More Obscure Languages
- Frege
- Jaskell
Further Reading
You may also want to read or watch these articles or videos:
- Functional Progamming in the Java Language, IBM DeveloperWorks (2004)
- Functional Programming Java, Lambda the Ultimate (2004)
- Functional Programming: a Pragmatic Introduction, InfoQ/CodePalousa (2011)
Taken from my P.SE answer to "Is Functional Programming Possible in Java?"