Contributing Java “Currying” in StackOverflow Documentation

A week or so ago I’ve contributed in the StackOverflow Documentation an entry about Currying  which until now it’s mostly intact and only cosmetic changes have been made to the original. This is how it goes:

Currying is the technique of translating the evaluation of a function that takes multiple arguments into evaluating a sequence of functions, each with a single argument.

This can be useful when:

  1. Different arguments of a function are calculated at different times. (see Example 1)
  2. Different arguments of a function are calculated by different tiers of the application. (see Example 2)

This generic utility applies currying on a 2-argument function:

class FunctionUtils {

    public static <A,B,C> Function<A,Function<B,C>> curry(BiFunction<A, B, C> f) {
        return a -> b -> f.apply(a,b);
    }

}

The above returned curried lambda expression can also be viewed/written written as:

a -> ( b -> f.apply(a,b) );

Example 1

Let’s assume that the total yearly income is a function composed by the income and a bonus:

BiFunction<Integer,Integer,Integer> totalYearlyIncome = (income,bonus) -> income + bonus;

Let’s assume that the yearly income portion is known in advance:

Function<Integer,Integer> partialTotalYearlyIncome = FunctionUtils.curry(totalYearlyIncome).apply(10000);

And at some point down the line the bonus is known:

System.out.println(partialTotalYearlyIncome.apply(100));

Example 2

Let’s assume that the car manufacturing involves the application of car wheels and car body:

BiFunction<String,String,String> carManufacturing = (wheels,body) -> wheels.concat(body);

These parts are applied by different factories:

class CarWheelsFactory {
    public Function<String,String> applyCarWheels(BiFunction<String,String,String> carManufacturing) {
        return FunctionUtils.curry(carManufacturing).apply("applied wheels..");
    }
}

class CarBodyFactory {
    public String applyCarBody(Function<String,String> partialCarWithWheels) {
        return partialCarWithWheels.apply("applied car body..");
    }
}

Notice that the CarWheelsFactory above curries the car manufacturing function and only applies the wheels. The car manufacturing process then will take the below form:

CarWheelsFactory carWheelsFactory = new CarWheelsFactory();
CarBodyFactory   carBodyFactory   = new CarBodyFactory();

BiFunction<String,String,String> carManufacturing = (wheels,body) -> wheels.concat(body);

Function<String,String> partialCarWheelsApplied = carWheelsFactory.applyCarWheels(carManufacturing);
String carCompleted = carBodyFactory.applyCarBody(partialCarWheelsApplied);

Install Java 6,7,8 on Mac OS X

Here’s a quick guide to have Java versions 6,7,8 installed on Mac OS X above and beyond.

Java 6

Java 6 is the last supported version provided by Apple. Therefore we’ll follow the Apple way to install the JDK although Java 1.6 can be branched out from Oracle or the OpenJDK project.

Successful installation will place Java 6 under /Library/Java/JavaVirtualMachines/1.6.0_37-b06-434.jdk/. Following that you can mark JAVA_HOME or point your IDE towards /Library/Java/JavaVirtualMachines/1.6.0_37-b06-434.jdk/Contents/Home/. Also to browse the JDK under an IDE point your editor to the JDK sources found in /Library/Java/JavaVirtualMachines/1.6.0_37-b06-434.jdk/Contents/Home/src.jar

Java 7

We need to go out in the wild to get JDK 7 installed on Mac OS X since mother-Apple doesn’t support it. We can download it either from Oracle or OpenJDK:

Successful installation will place Java 7 under /Library/Java/JavaVirtualMachines/jdk1.7.0_07.jdk/. Following that you can mark JAVA_HOME or point your IDE towards /Library/Java/JavaVirtualMachines/jdk1.7.0_07.jdk/Contents/Home/. Also to browse the JDK under an IDE point your editor to the JDK sources found in /Library/Java/JavaVirtualMachines/jdk1.7.0_07.jdk/Contents/Home/src.jar

Java 8

We can get a copy of the latest snapshot of JDK 8 to play around with the lambda expressions (natively supported on IntelliJ IDEA) if you haven’t tried already closures in Groovy, Scala or just Predicates/Functions in Google Guava from this download resource

Successful installation will place Java 8 under /Library/Java/JavaVirtualMachines/jdk1.8.0.jdk/. Following that you can mark JAVA_HOME or point your IDE towards /Library/Java/JavaVirtualMachines/jdk1.8.0.jdk/Contents/Home/. Also to browse the JDK under an IDE point your editor to the JDK sources found in /Library/Java/JavaVirtualMachines/jdk1.8.0.jdk/Contents/Home/src.jar