A concise and declarative way to define lambda expressions in Java. It builds on top of Guava and provides an alternative to anonymous inner classes for defining Functions and Predicates when those functors just wrap single object-level method invocations.
These examples are completely fleshed out in the unit tests.
Let's say you have a collection of matrices and you want to retain only the ones that are square. Guava makes this easy:
com.google.common.collect.Collections2.filter(matrices, matrixIsSquare);
Typically the matrixIsSquare Predicate would be defined like this:
Predicate<AnyMatrix> matrixIsSquare = new Predicate<AnyMatrix>() {
public boolean apply(AnyMatrix input) {
return input.isSquare();
}
};
It seems excessive for just a simple method invocation. fluent-lambda provides a concise and declarative way of defining the same Predicate:
Predicate<AnyMatrix> matrixIsSquare = forMethod(ofClass(AnyMatrix.class).isSquare());
It uses an example method invocation to define the Predicate. There is no need for referencing classes or methods by name, so it is refactoring-friendly.
Let's say you have a collection of vectors that you would like to get the norms of those vectors. Again, Guava makes this easy:
com.google.common.collect.Lists.transform(vectors, normCalculator);
Typically, the normCalculator Function would be defined like this:
Function<RealVector, Double> normCalculator = new Function<RealVector, Double>() {
public Double apply(RealVector input) {
return input.getNorm();
}
};
This also seems excessive for just a simple method invocation. fluent-lambda provides a concise and declarative way of defining Functions as well:
Function<RealVector, Double> normCalculator = forMethod(ofClass(RealVector.class).getNorm());
fluent-lambda is available in The Central Repository.
To include the library in your Maven project, add the following dependency to your POM:
<dependency>
<groupId>com.macasaet.lambda</groupId>
<artifactId>fluent-lambda</artifactId>
<version>0.2.1</version>
</dependency>
To include the library in your Gradle project, add the following dependency to build.gradle:
compile group: 'com.macasaet.lambda', name: 'fluent-lambda', version: '0.1.2'
You can download the library directly from The Central Repository.
fluent-lambda uses the Gradle build system. To build from the command line, type:
./gradlew install
fluent-lambda currently does not build with JDK 6.
Before releasing to Central, you will need to define the following properties in your build.gradle file:
signing.keyId=[your eight-character public key ID]
signing.secretKeyRingFile=[full path to the secret key ring that contains the keyId]
nexusUsername=[your username for Central]
To build and release to Central, type:
./gradlew --no-daemon uploadArchives
You will be prompted for the password to your secret key and for your Central password.