Skip to content

Commit 73677ad

Browse files
author
Arpit Aggarwal
committed
Java-8 Functions Composition
1 parent d07c133 commit 73677ad

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package com.arpit.java8.function;
2+
3+
import java.util.function.Function;
4+
5+
public class FunctionComposition {
6+
7+
private Function<Integer, Integer> times2 = e -> e * 2;
8+
9+
private Function<Integer, Integer> squared = e -> e * e;
10+
11+
/**
12+
* Reference :
13+
* http://www.deadcoderising.com/2015-09-07-java-8-functional-composition-
14+
* using-compose-and-andthen/
15+
*
16+
* @param args
17+
*/
18+
public static void main(String[] args) {
19+
FunctionComposition obj = new FunctionComposition();
20+
obj.run();
21+
22+
}
23+
24+
/**
25+
* While the compose function executes the caller last and the parameter
26+
* first, the andThen executes the caller first and the parameter last.
27+
*/
28+
private void run() {
29+
int result = times2.compose(squared).apply(4); // Returns 32
30+
System.out.println(result);
31+
int result1 = times2.andThen(squared).apply(4); // Returns 64
32+
System.out.println(result1);
33+
}
34+
35+
}

0 commit comments

Comments
 (0)