Skip to content

Commit 5fdf341

Browse files
author
sergfrolov
committed
Added Java implementation
Added Java implementation
1 parent d30dccb commit 5fdf341

File tree

4 files changed

+68
-0
lines changed

4 files changed

+68
-0
lines changed

Diff for: .gitignore

+4
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
11
node_modules
22
*.log
33
.DS_Store
4+
# IntelliJ
5+
/out/
6+
/.idea
7+
*.iml

Diff for: Java/src/1-closure.java

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import java.util.function.*;
2+
3+
class Closure {
4+
5+
public static void main(String[] args) {
6+
Function<Double,Function<Double,Double>> createLog = (Double base) -> (Double n) -> log(base, n);
7+
Function<Double,Double> lg = createLog.apply(10d);
8+
Function<Double,Double> ln = createLog.apply(Math.E);
9+
//usage
10+
System.out.println(lg.apply(1000d));
11+
System.out.println(ln.apply(Math.E * Math.E));
12+
}
13+
14+
private static Double log(Double base, Double n)
15+
{
16+
return Math.log(n)/ Math.log(base);
17+
}
18+
19+
}

Diff for: Java/src/2-lambda.java

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import java.util.function.Function;
2+
3+
class Lambda {
4+
public static void main(String[] args) {
5+
Function<Double,Double> lg = (Double n) -> log(10d, n);
6+
Function<Double,Double> ln = (Double n) -> log(Math.E, n);
7+
//usage
8+
System.out.println(lg.apply(1000d));
9+
System.out.println(ln.apply(Math.E*Math.E));
10+
}
11+
12+
private static Double log(Double base, Double n)
13+
{
14+
return Math.log(n)/ Math.log(base);
15+
}
16+
}

Diff for: Java/src/4-curry.java

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import java.util.function.Function;
2+
3+
class Curry {
4+
public static void main(String[] args) {
5+
final TriFunction<Integer,Integer,Integer,Integer> add = Curry::sum;
6+
Function<Integer, Function<Integer, Function<Integer,Integer>>> uncurry = Curry.curry(add);
7+
TriFunction<Integer,Integer,Integer,Integer> curry = Curry.uncurry(uncurry);
8+
//usage
9+
System.out.println("Curried result : " + curry.apply(1,2,3));
10+
System.out.println("Uncurried result : " + uncurry.apply(1).apply(2).apply(3));
11+
}
12+
private static Integer sum(Integer a, Integer b, Integer c){
13+
return a + b + c;
14+
}
15+
16+
private static <A, B, C, D> Function<A, Function<B, Function<C,D>>> curry(final TriFunction<A, B, C, D> f) {
17+
return (A a) -> (B b) -> (C c) -> f.apply(a, b, c);
18+
}
19+
20+
private static <A, B, C, D> TriFunction<A,B,C,D> uncurry(Function<A, Function<B, Function<C, D>>> f) {
21+
return (A a, B b, C c) -> f.apply(a).apply(b).apply(c);
22+
}
23+
24+
@FunctionalInterface
25+
public interface TriFunction<A,B,C,D>{
26+
D apply(A a,B b,C c);
27+
}
28+
29+
}

0 commit comments

Comments
 (0)