Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
38
Task/Currying/Java/currying-1.java
Normal file
38
Task/Currying/Java/currying-1.java
Normal file
|
|
@ -0,0 +1,38 @@
|
|||
public class Currier<ARG1, ARG2, RET> {
|
||||
public interface CurriableFunctor<ARG1, ARG2, RET> {
|
||||
RET evaluate(ARG1 arg1, ARG2 arg2);
|
||||
}
|
||||
|
||||
public interface CurriedFunctor<ARG2, RET> {
|
||||
RET evaluate(ARG2 arg);
|
||||
}
|
||||
|
||||
final CurriableFunctor<ARG1, ARG2, RET> functor;
|
||||
|
||||
public Currier(CurriableFunctor<ARG1, ARG2, RET> fn) { functor = fn; }
|
||||
|
||||
public CurriedFunctor<ARG2, RET> curry(final ARG1 arg1) {
|
||||
return new CurriedFunctor<ARG2, RET>() {
|
||||
public RET evaluate(ARG2 arg2) {
|
||||
return functor.evaluate(arg1, arg2);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
Currier.CurriableFunctor<Integer, Integer, Integer> add
|
||||
= new Currier.CurriableFunctor<Integer, Integer, Integer>() {
|
||||
public Integer evaluate(Integer arg1, Integer arg2) {
|
||||
return new Integer(arg1.intValue() + arg2.intValue());
|
||||
}
|
||||
};
|
||||
|
||||
Currier<Integer, Integer, Integer> currier
|
||||
= new Currier<Integer, Integer, Integer>(add);
|
||||
|
||||
Currier.CurriedFunctor<Integer, Integer> add5
|
||||
= currier.curry(new Integer(5));
|
||||
|
||||
System.out.println(add5.evaluate(new Integer(2)));
|
||||
}
|
||||
}
|
||||
34
Task/Currying/Java/currying-2.java
Normal file
34
Task/Currying/Java/currying-2.java
Normal file
|
|
@ -0,0 +1,34 @@
|
|||
import java.util.function.BiFunction;
|
||||
import java.util.function.Function;
|
||||
|
||||
public class Curry {
|
||||
|
||||
//Curry a method
|
||||
public static <T, U, R> Function<T, Function<U, R>> curry(BiFunction<T, U, R> biFunction) {
|
||||
return t -> u -> biFunction.apply(t, u);
|
||||
}
|
||||
|
||||
public static int add(int x, int y) {
|
||||
return x + y;
|
||||
}
|
||||
|
||||
public static void curryMethod() {
|
||||
BiFunction<Integer, Integer, Integer> bif = Curry::add;
|
||||
Function<Integer, Function<Integer, Integer>> add = curry(bif);
|
||||
Function<Integer, Integer> add5 = add.apply(5);
|
||||
System.out.println(add5.apply(2));
|
||||
}
|
||||
|
||||
//Or declare the curried function in one line
|
||||
public static void curryDirectly() {
|
||||
Function<Integer, Function<Integer, Integer>> add = x -> y -> x + y;
|
||||
Function<Integer, Integer> add5 = add.apply(5);
|
||||
System.out.println(add5.apply(2));
|
||||
}
|
||||
|
||||
//prints 7 and 7
|
||||
public static void main(String[] args) {
|
||||
curryMethod();
|
||||
curryDirectly();
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue