views:

1132

answers:

7

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 reuse a library here.

+7  A: 

Functional Java is one that's worth taking a look at and FunctionalJ is another.

mikej
+1 for FunctioanlJ
dfa
+1 for Functional Java
Apocalisp
So .. which one?Also, for the lazy among us, can they be used without Java closures? (Assuming I only want a library, not something that forces me to use a different compiler)
ripper234
Functional Java is just a library, but it works with the BGGA closure syntax if you want to use that.
Apocalisp
+10  A: 

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.

dfa
But BGGA is not actually in Java. So this won't compile, right?
Gabe Moothart
BGGA is available as a precompiler that translates {int i => i + 42} into new F<Integer, Integer>() { public Integer f(Integer i) {return i + 42; }}. The latter will compile with any old Java.
Apocalisp
right, it is a little frustrating indeed
dfa
+7  A: 

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

It also has a bunch of collection classes as well!

Chi
+5  A: 

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.

CrazyJugglerDrummer
+2  A: 

Jambda is another FP-library. From the documentation:

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.

Johan Kullbom
+1  A: 

If you want a pure Java solution check out 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

Mario Fusco
+3  A: 

I suggest bolts:

http://bitbucket.org/stepancheg/bolts/

Unlike similar libraries, JDK standard collections are wrapped, and functional operations (map, filter, reduce etc.) are methods of collection interfaces. It is convenient.

stepancheg
hehe... +1 just for the shameless self promotion:)
dsmith
@dsmith It is still the answer :)
stepancheg