A-M baby
This commit is contained in:
parent
764da6cbbb
commit
db842d013d
19005 changed files with 197040 additions and 7 deletions
33
Task/Function-composition/Java/function-composition-1.java
Normal file
33
Task/Function-composition/Java/function-composition-1.java
Normal file
|
|
@ -0,0 +1,33 @@
|
|||
public class Compose {
|
||||
|
||||
// Java doesn't have function type so we define an interface
|
||||
// of function objects instead
|
||||
public interface Fun<A,B> {
|
||||
B call(A x);
|
||||
}
|
||||
|
||||
public static <A,B,C> Fun<A,C> compose(final Fun<B,C> f, final Fun<A,B> g) {
|
||||
return new Fun<A,C>() {
|
||||
public C call(A x) {
|
||||
return f.call(g.call(x));
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
Fun<Double,Double> sin = new Fun<Double,Double>() {
|
||||
public Double call(Double x) {
|
||||
return Math.sin(x);
|
||||
}
|
||||
};
|
||||
Fun<Double,Double> asin = new Fun<Double,Double>() {
|
||||
public Double call(Double x) {
|
||||
return Math.asin(x);
|
||||
}
|
||||
};
|
||||
|
||||
Fun<Double,Double> sin_asin = compose(sin, asin);
|
||||
|
||||
System.out.println(sin_asin.call(0.5)); // prints "0.5"
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue