A-M baby
This commit is contained in:
parent
764da6cbbb
commit
db842d013d
19005 changed files with 197040 additions and 7 deletions
36
Task/First-class-functions/Java/first-class-functions-2.java
Normal file
36
Task/First-class-functions/Java/first-class-functions-2.java
Normal file
|
|
@ -0,0 +1,36 @@
|
|||
import java.util.ArrayList;
|
||||
import java.util.function.Function;
|
||||
|
||||
public class FirstClass{
|
||||
|
||||
public static <A,B,C> Function<A, C> compose(
|
||||
final Function<B, C> f, final Function<A, B> g) {
|
||||
return new Function<A, C>() {
|
||||
@Override public C apply(A x) {
|
||||
return f.apply(g.apply(x));
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
public static void main(String[] args){
|
||||
ArrayList<Function<Double, Double>> functions = new ArrayList<>();
|
||||
|
||||
functions.add(Math::cos);
|
||||
functions.add(Math::tan);
|
||||
functions.add(x -> x * x);
|
||||
|
||||
ArrayList<Function<Double, Double>> inverse = new ArrayList<>();
|
||||
|
||||
inverse.add(Math::acos);
|
||||
inverse.add(Math::atan);
|
||||
inverse.add(Math::sqrt);
|
||||
System.out.println("Compositions:");
|
||||
for(int i = 0; i < functions.size(); i++){
|
||||
System.out.println(compose(functions.get(i), inverse.get(i)).apply(0.5));
|
||||
}
|
||||
System.out.println("Hard-coded compositions:");
|
||||
System.out.println(Math.cos(Math.acos(0.5)));
|
||||
System.out.println(Math.tan(Math.atan(0.5)));
|
||||
System.out.println(Math.pow(Math.sqrt(0.5), 2));
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue