Skip to content

Instantly share code, notes, and snippets.

Last active Aug 19, 2016
What would you like to do?
import java.util.function.Function;
public class FunctionalChaining {
public static void main(String[] args)
Function<Double, Double> func1 = d -> d + 0.2;
Function<Double, Double> func2 = d -> d + 0.5;
// Simple chaining
// Chaining using Stream.of
Function<Double, Double> combined = Stream.of(func1, func2)
.reduce(Function.identity(), Function::andThen);
// Pass it to a function which can accept variable number of functions.
System.out.println(combineFunctions(func1, func2));
private static Double combineFunctions(Function<Double, Double>... combineFunctions)
Function<Double, Double> chainedFunction = Stream.of(combineFunctions)
.reduce(function -> function,Function::andThen);
return chainedFunction.apply(1.0);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment