Clone wiki

bolts / Function

Home * На русском

Function Objects

Function in bolts is a class that inherits one of abstact Function* class. Function* classes are:

Function — simplest, proper, main class. It describes single argument function that returns object. All another Function* classes are particular cases of functions.

All abstract function Function* classes has exactly one abstract apply method, and all classes are distinguished by the method signature.

About Apply method

Function<P1, R>.apply method signature is R apply(P1).

For rest Function* classes the following rules apply:

Digit in class name shows "apply" method parameter count (function parameter count).

Letter (if present) defines the return value type: B stands for boolean, I for int, V for void.

For instance:

Function2I<P1, P2> class has int apply(P1, P2) method

Function1B<P1> class has boolean apply(P1) method

Simple Example

You do not need to understand all the rules. In 99% cases only two function classes are used:

Function — is a mapper (or transformer), function that transforms object

Function1B — is a predicate — function that tells that some statement about object is either true or false

How to Construct Function Objects

Function constructon ways are: anonymous methods writing, another functions combination and argument binding.

Anonymous class

Two functions example.

We have User class with "String login" field. And we need a function that fetches login from the user. Usually we will write it like this:

class User {
  String login;
  public static Function<User, String> loginF() {
    return new Function<User, String>() {
      public String apply(User user) {
        return user.login;

It is plenty of code here, but modern IDEs (Idea, Eclipse) simplify the task greatly.

Another function example: predicate that checks if passed string is empty:

static Function1B<String> emptyF() {
  return new Function1B<String>() {
    public boolean apply(String string) {
      return string == null || string.length == 0;

Function combination

We need a predicate that tests if users has non-empty login.

This can be achieved by combining functions described above:


Function composition is a very powerful mechanism.

Function Argument Binding

Function with N arguments can be transformed to function with N-1 arguments by binding fixed value to some function argument. All Function* classes have bindK methods, 0 <= L < N.

Example. Let us have function that does string concatenation:

Function2<String, String, String> plusF() {
  return new Function2<String, String, String>() {
    public void apply(String a, String b) {
      return a + b;

plusF is a two argument function. It can be transformed to the function with single argument that adds prefix to string:

plusF().bind1("(") // function that adds brace prefix
plusF().bind2(")") // function that adds brace suffix

So function that surrounds string with braces may look like this:



Functions are used for collection operations.