bad.robot

good robots do what they're told

Currying Functions in Java & Scala

Currying is the technique of transforming a function with multiple arguments into a function with just one argument. The single argument is the value of the first argument from the original function and the function returns another single argument function. This in turn would take the second original argument and itself return another single argument function. This chaining continues over the number of arguments of the original. The last in the chain will have access to all of the arguments and so can do whatever it needs to do.

You can turn any function with multiple arguments into it’s curried equivalent. Let’s have a look at this in action.

Java

For example, in Java, you can convert

public static int add(int a, int b) {
    return a + b;
}

into something like this (where Function<A, B> defines a single method B apply(A a)).

public static Function<Integer, Function<Integer, Integer>> add() {
    return new Function<Integer, Function<Integer, Integer>>() {
        @Override
        public Function<Integer, Integer> apply(final Integer x) {
            return new Function<Integer, Integer>() {
                @Override
                public Integer apply(Integer y) {
                    return x + y;
                }
            };
        }
    };
}

Calling the original method

add(1, 1);                       // gives 2

and calling the curried version

add();                          // gives back a instance of Function<[A, B]>
add().apply(1);                 // gives back a instance of Function<[A, B]>
add().apply(1).apply(1)         // gives 2

Java 8

In Java 8, it’s much less verbose using the new lambda syntax.

public static Function<Integer, Function<Integer, Integer>> add() {
    return x -> y -> x + y;
}

Scala

In Scala, the regular uncurried function would look like this.

def add(x: Int, y: Int): Int = {
  x + y
}

As Scala supports curried functions, you can turn this into it’s curried version simply by separating out the arguments.

// shorthand
def add(x: Int)(y: Int): Int = {
  x + y
}

Which is shorthand for writing it out like this.

// longhand
def add(x: Int): (Int => Int) = {
  (y: Int) => {
    x + y
  }
}

Using the REPL to show how they’re called;

scala> def add(x: Int)(y: Int): Int = {
     |   x + y
     | }
add: (x: Int)(y: Int)Int

scala> add(1) _
res1: Int => Int = <Function>

scala> (add(1) _).apply(1)
res2: Int = 2

scala> add(1)(1)
res3: Int = 2

and working with the longhand version;

scala> def add2(x: Int): (Int => Int) = {
     |   (y: Int) => {
     |     x + y
     |   }
     | }
add2: (x: Int)Int => Int

scala> add2(1).apply(1)
res4: Int = 2

It turns out that it’s this partial application of functions that’s really interesting. Currying in Scala allows us to defer execution and reuse functions. We’ll have a look at that in the next article.

More Information

Over to you...