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:

User.loginF().andThen(StringUtils.emptyF()).notF();

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:

plusF().bind1("(").andThen(plusF().bind2(")"))

Collections

Functions are used for collection operations.

Updated

Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.