Example usage for java.util.function Function compose

List of usage examples for java.util.function Function compose

Introduction

In this page you can find the example usage for java.util.function Function compose.

Prototype

default <V> Function<V, R> compose(Function<? super V, ? extends T> before) 

Source Link

Document

Returns a composed function that first applies the before function to its input, and then applies this function to the result.

Usage

From source file:Main.java

public static void main(String[] args) {
    Function<Integer, Integer> incrementer = (n) -> n + 1;
    Function<Integer, Integer> before = (n) -> n * 2;
    System.out.println(incrementer.compose(before).apply(2));
}

From source file:Main.java

public static void main(String[] args) {
    Function<Integer, String> converter = (i) -> Integer.toString(i);

    Function<String, Integer> reverse = (s) -> Integer.parseInt(s);

    System.out.println(converter.apply(3).length());
    System.out.println(converter.compose(reverse).apply("30").length());
}

From source file:Main.java

public static void main(String[] args) {
    Function<Double, Double> square = number -> number * number;
    Function<Double, Double> half = number -> number * 2;

    List<Double> numbers = Arrays.asList(10D, 4D, 12D);

    // pay attention to the order
    System.out.println(mapIt(numbers, square.compose(half)));
    System.out.println(mapIt(numbers, half.compose(square)));

    // you can chain them
    System.out.println(mapIt(numbers, half.andThen(square)));

}