diff --git a/.gitignore b/.gitignore index ac1e8f7..743d04a 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,7 @@ node_modules *.log .DS_Store +# IntelliJ +/out/ +/.idea +*.iml \ No newline at end of file diff --git a/Java/src/1-closure.java b/Java/src/1-closure.java new file mode 100644 index 0000000..ff3b55f --- /dev/null +++ b/Java/src/1-closure.java @@ -0,0 +1,19 @@ +import java.util.function.*; + +class Closure { + + public static void main(String[] args) { + Function<Double,Function<Double,Double>> createLog = (Double base) -> (Double n) -> log(base, n); + Function<Double,Double> lg = createLog.apply(10d); + Function<Double,Double> ln = createLog.apply(Math.E); + //usage + System.out.println(lg.apply(1000d)); + System.out.println(ln.apply(Math.E * Math.E)); + } + + private static Double log(Double base, Double n) + { + return Math.log(n)/ Math.log(base); + } + +} diff --git a/Java/src/2-lambda.java b/Java/src/2-lambda.java new file mode 100644 index 0000000..0f537b2 --- /dev/null +++ b/Java/src/2-lambda.java @@ -0,0 +1,16 @@ +import java.util.function.Function; + +class Lambda { + public static void main(String[] args) { + Function<Double,Double> lg = (Double n) -> log(10d, n); + Function<Double,Double> ln = (Double n) -> log(Math.E, n); + //usage + System.out.println(lg.apply(1000d)); + System.out.println(ln.apply(Math.E*Math.E)); + } + + private static Double log(Double base, Double n) + { + return Math.log(n)/ Math.log(base); + } +} diff --git a/Java/src/4-curry.java b/Java/src/4-curry.java new file mode 100644 index 0000000..09125f7 --- /dev/null +++ b/Java/src/4-curry.java @@ -0,0 +1,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); + } + +}