As I have started to invest in using functional idioms in Java, I
discovered that oftentimes the creation of function-type objects like
a Functor often add as much noise and complexity to the source code as
they reduce when they are used to replace for-loops. This bothered
me, since often the function I am trying to create is simply a wrapper
around a simple method that already exists. I know Jedi has the very
nice @Jedi/@Sith annotations to help, but some project teams may wish
to avoid the usage of APT that complicates project builds and requires
adherence to a strict naming convention or else using String-args in
the annotation which are harder to refactor using tools.
So I have created a library, Funcito, to simplify function creation in
existing functional libraries such as Jedi Core. It is found at:
http://funcito.googlecode.com
As an example take the following code before Funcito (and assuming non-
usage of @Jedi*):
Functor<String, Integer> lengthFunction = new Functor<String,
Integer>() {
public Integer execute(String string) {
return string.length();
}
};
With Funcito this gets replaced by:
Functor<String, Integer> lengthFunction =
functorFor( callsTo(CharSequnce.class).length() );
If you have more than one method you want to wrap on the same
interface or class, notation can be expressed even more concise and
readable:
CharSequence callsTo = callsTo(CharSequence.class);
Functor<CharSequence, Integer> length =
functorFor( callsTo.length() );
Functor<CharSequence, String> toString =
functorFor( callsTo.toString() );