Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
node_modules
*.log
.DS_Store
# IntelliJ
/out/
/.idea
*.iml
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add extra line break at EOL

19 changes: 19 additions & 0 deletions Java/src/1-closure.java
Original file line number Diff line number Diff line change
@@ -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);
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code style. And could you split this line to fit 100 symbols in line, please? Just a code style suggestion.

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)
{
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code style.

return Math.log(n)/ Math.log(base);
}

}
16 changes: 16 additions & 0 deletions Java/src/2-lambda.java
Original file line number Diff line number Diff line change
@@ -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);
}
}
29 changes: 29 additions & 0 deletions Java/src/4-curry.java
Original file line number Diff line number Diff line change
@@ -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);
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code formatting.

//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) {
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you split this line to fit 100 symbols in line, please? Just a code style suggestion.

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) {
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you split this line to fit 100 symbols in line, please? Just a code style suggestion.

return (A a, B b, C c) -> f.apply(a).apply(b).apply(c);
}

@FunctionalInterface
public interface TriFunction<A,B,C,D>{
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code formatting.

D apply(A a,B b,C c);
}

}