-
-
Notifications
You must be signed in to change notification settings - Fork 62
/
Copy path4-curry.java
29 lines (24 loc) · 1.08 KB
/
4-curry.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
import java.util.function.Function;
class Curry {
public static void main(String[] args) {
final TriFunction<Integer,Integer,Integer,Integer> add = Curry::sum;
Function<Integer, Function<Integer, Function<Integer,Integer>>> uncurry = Curry.curry(add);
TriFunction<Integer,Integer,Integer,Integer> curry = Curry.uncurry(uncurry);
//usage
System.out.println("Curried result : " + curry.apply(1,2,3));
System.out.println("Uncurried result : " + uncurry.apply(1).apply(2).apply(3));
}
private static Integer sum(Integer a, Integer b, Integer c){
return a + b + c;
}
private static <A, B, C, D> Function<A, Function<B, Function<C,D>>> curry(final TriFunction<A, B, C, D> f) {
return (A a) -> (B b) -> (C c) -> f.apply(a, b, c);
}
private static <A, B, C, D> TriFunction<A,B,C,D> uncurry(Function<A, Function<B, Function<C, D>>> f) {
return (A a, B b, C c) -> f.apply(a).apply(b).apply(c);
}
@FunctionalInterface
public interface TriFunction<A,B,C,D>{
D apply(A a,B b,C c);
}
}