-
Notifications
You must be signed in to change notification settings - Fork 2
/
ExampleSession5.java
76 lines (63 loc) · 2.53 KB
/
ExampleSession5.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package functionalprogramming;
import java.util.ArrayList;
import java.util.List;
import java.util.function.Function;
/**
*
* @author abdalla
*/
public class ExampleSession5 {
private static List<Double> Mydata = new ArrayList<Double>() {
{
add((double) 3);
add((double) 5);
add((double) 7);
add((double) 8);
}
};
public static Function<Double, Double> MyComposedFunction =
ComposeFunction(x -> Addone(x), x -> Square(x), x -> SubtractTen(x));
public static void main(String[] args) {
Mydata.stream().map(x -> Addone(x)).map(x -> Square(x)).map(x -> SubtractTen(x)).forEach(System.out::println);
System.out.println("====================================");
Mydata.stream().map(x -> SubtractTen(Square(Addone(x)))).forEach(System.out::println);
System.out.println("====================================");
Mydata.stream().map(x -> MyComposedFunction.apply(x)).forEach(System.out::println);
System.out.println("====================================");
Mydata.stream().map(AddoneSquareSubtractTen()).forEach(System.out::println);
}
public static Function<Double, Double> Test() {
return x -> x + 1;
}
public static double Addone(double x) {
return x + 1;
}
public static double Square(double x) {
return Math.pow(x, 2);
}
public static double SubtractTen(double x) {
return x - 10;
}
public static Function<Double, Double> ComposeFunction(Function<Double, Double> F1, Function<Double, Double> F2, Function<Double, Double> F3) {
return (x)
-> {
return F3.apply(F2.apply(F1.apply(x)));
};
}
public static Function<Double, Double> AddoneSquareSubtractTen() {
Function< Double, Double> q1 = x -> Addone(x);
Function< Double, Double> q2 = x -> Square(x);
Function< Double, Double> q3 = x -> SubtractTen(x);
return q1.andThen(q2).andThen(q3);
}
//Compose() C# example is a built in function in java = andThen() no need to implement it and to make reverse action use compose() in java
// public static Function<T1, T3> Compose<T1, T2, T3>(this Func<T1, T2> f,Func<T2, T3> g)
// {
// return (x) => g(f(x));
// }
}