Currying: Difference between revisions
Content deleted Content added
Line 606: | Line 606: | ||
public class Curry { |
public class Curry { |
||
//Curry a method |
|||
public static <T, U, R> Function<T, Function<U, R>> curry(BiFunction<T, U, R> biFunction) { |
public static <T, U, R> Function<T, Function<U, R>> curry(BiFunction<T, U, R> biFunction) { |
||
return t -> u -> biFunction.apply(t, u); |
return t -> u -> biFunction.apply(t, u); |
||
Line 614: | Line 615: | ||
} |
} |
||
public static void |
public static void curryMethod() { |
||
BiFunction<Integer, Integer, Integer> bif = Curry::add; |
BiFunction<Integer, Integer, Integer> bif = Curry::add; |
||
Function<Integer, Function<Integer, Integer>> |
Function<Integer, Function<Integer, Integer>> add = curry(bif); |
||
Function<Integer, Integer> add5 = |
Function<Integer, Integer> add5 = add.apply(5); |
||
System.out.println(add5.apply(2)); |
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(); |
|||
} |
} |
||
} |
} |