2015-12-02 09:20:27 -08:00
|
|
|
// functional/CurryingAndPartials.java
|
2015-12-15 11:47:04 -08:00
|
|
|
// (c)2016 MindView LLC: see Copyright.txt
|
2015-12-02 09:20:27 -08:00
|
|
|
// We make no guarantees that this code is fit for any purpose.
|
2016-09-23 13:23:35 -06:00
|
|
|
// Visit http://OnJava8.com for more book information.
|
2015-12-02 09:20:27 -08:00
|
|
|
import java.util.function.*;
|
|
|
|
|
|
|
|
public class CurryingAndPartials {
|
|
|
|
// Uncurried:
|
|
|
|
static String uncurried(String a, String b) {
|
|
|
|
return a + b;
|
|
|
|
}
|
|
|
|
public static void main(String[] args) {
|
|
|
|
// Curried function:
|
|
|
|
Function<String, Function<String, String>> sum =
|
2016-11-21 12:37:57 -08:00
|
|
|
a -> b -> a + b; // [1]
|
2015-12-02 09:20:27 -08:00
|
|
|
|
|
|
|
System.out.println(uncurried("Hi ", "Ho"));
|
|
|
|
|
|
|
|
Function<String, String>
|
2016-11-21 12:37:57 -08:00
|
|
|
hi = sum.apply("Hi "); // [2]
|
2015-12-02 09:20:27 -08:00
|
|
|
System.out.println(hi.apply("Ho"));
|
|
|
|
|
|
|
|
// Partial application:
|
|
|
|
Function<String, String> sumHi = sum.apply("Hup ");
|
|
|
|
System.out.println(sumHi.apply("Ho"));
|
|
|
|
System.out.println(sumHi.apply("Hey"));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/* Output:
|
|
|
|
Hi Ho
|
|
|
|
Hi Ho
|
|
|
|
Hup Ho
|
|
|
|
Hup Hey
|
|
|
|
*/
|