Mango defines a variety of function types
Generators
A Generator describes a function which takes no arguments - fn(). It returns some Object, and may return the same object or different objects for each invocation. It can refer to local state, perform disk reads or writes, or whatever.
Unary Functions
A UnaryFunction describes a function which takes one argument - fn(x). It returns some Object, and may return the same object or different objects for each invocation, even given the same argument.
Binary Functions
A BinaryFunction is some function taking two arguments - fn(x, y). It returns some Object, and may return the same object or different objects for each invocation, even given the same arguments.
Predicates
A Predicate is a special form of unary function, whose result represents the truth or otherwise of some condition. It returns true if the condition the Predicate tests for is satisfied, false otherwise.
Predicates include And, Or, Not, True, and False.
Binary Predicates
A BinaryPredicate is some function taking two arguments - fn(x, y) and returning the result of some test. It returns true if the test is met, false otherwise.
Binary predicates include EqualTo, NotEqualTo, GreaterThan, GreaterThanEquals, LessThan, LessThanEquals, Not, And and Or.
Adaptors
Many Mango algorithms take a UnaryFunction argument. Often though, the thing you'd really like to pass is a method on another object or a static class method. Mango's Adapt methods wrap up object methods into a UnaryFunction. For instance
Mango.forEach(list, Bind.Method(System.out, "println"));is equivalent to
class printOut implements UnaryFunction
{
public Object fn(Object x)
{
System.out.println(x);
}
}
...
Mango.forEach(list, new printOut());
is equivalent to
Mango.forEach (list, new UnaryFunction () {
public Object fn (Object x)
{
System.out.println (x);
return x;
}
});
All these are both equivalent to
for(int i = 0; i < list.size(); ++i) System.out.println(list.get(i));Obviously, this is a bit of a silly example, but you get the picture.
Binders
A binder converts a binary function (or predicate) into a unary function (or predicate) by fixing one of its inputs to a constant value. See the Javadoc for more information.