OnJava8-Examples/functional/CurryingAndPartials.java

36 lines
915 B
Java
Raw Normal View History

2015-12-02 09:20:27 -08:00
// functional/CurryingAndPartials.java
2020-10-07 13:35:40 -06:00
// (c)2020 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 =
a -> b -> a + b; // [1]
2015-12-02 09:20:27 -08:00
System.out.println(uncurried("Hi ", "Ho"));
Function<String, String>
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 ");
2015-12-02 09:20:27 -08:00
System.out.println(sumHi.apply("Ho"));
System.out.println(sumHi.apply("Hey"));
}
}
/* Output:
Hi Ho
Hi Ho
Hup Ho
Hup Hey
*/