Contributing Scala “When to use Currying” in StackOverflow Documentation

A week or so ago I’ve contributed in StackOverflow Documentation an entry about Currying under the Scala Language tag. I’ve repeated the same exercise for Java Currying Documentation. 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 is normally useful when for example:

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

Example 1

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

val totalYearlyIncome😦Int,Int) => Int =  (income, bonus) => income + bonus

The curried version of the above 2-arity function is:

val totalYearlyIncomeCurried: Int => Int => Int = totalYearlyIncome.curried

Note in the above definition that the type can be also viewed/written as:

Int => (Int => Int)

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

val partialTotalYearlyIncome: Int => Int = totalYearlyIncomeCurried(10000)

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

partialTotalYearlyIncome(100)

Example 2

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

val carManufacturing😦String,String) => String = (wheels, body) => wheels + body

These parts are applied by different factories:

class CarWheelsFactory {
  def applyCarWheels(carManufacturing😦String,String) => String): String => String =
          carManufacturing.curried("applied wheels..")
}
    
class CarBodyFactory {
  def applyCarBody(partialCarWithWheels: String => String): String = partialCarWithWheels("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:

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

val carManufacturing😦String,String) => String = (wheels, body) => wheels + body
  
val partialCarWheelsApplied: String => String  = carWheelsFactory.applyCarWheels(carManufacturing)
val carCompleted = carBodyFactory.applyCarBody(partialCarWheelsApplied)

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);

Visitor Design Pattern in Scala

Scala provides built-in support for the Visitor Design Pattern through the use of pattern matching.

The Visitor Design Pattern is trying to address the problem of never-ending new functionality that is otherwise implemented by adding new methods in the inheritance tree.

According to the Visitor pattern the inheritance tree is decoupled from a new functionality and encapsulated in a separate object that packs all implementations according to the inheritance tree normally by using method overloading of all the various types.

In Scala by the use of the built-in pattern matching this becomes very easy:

class Animal { def walk:String }

class Dog extends Animal { override def walk = "on 4" }

class Man extends Animal { override def walk = "on 2" }

/**
 *  Visitor Pattern provides a solution to never-ending new
 *  functionality that would otherwise be implemented with new
 *  method in the hierarchy tree.
 */
def talk(animal: Animal) = animal match {
  case Dog => "wav wav"
  case Man => "hi"
}

/**
 *  New functionality implemented in separate methods that
 *  uses pattern matching to implement tailored functionality.
 */
def swim(animal: Animal) = animal match {
  case Dog => "on 4"
  case Man => "on 4"
}