Friday, February 21, 2014

Call by name using Java 8 Supplier

A call by name example in the Functional programming course offered through Coursera is the following:

def loop: Boolean = loop 

def and(x: =>Boolean, y: =>Boolean) = if (x) y else false

and(false, loop)  

Here loop is a non-terminating recursive call. "and" is a function which takes two call by name expressions, and when "and" is called with "loop" as a parameter the call does not blow up, as loop is evaluated only when required.

Now, a similar code, though not this elegant, can be written using Java 8 Lambda expressions.

First the non-recursive looping expression:

 Boolean loop() {
  return loop();

The following "and" method simulates call by name semantics by taking Java 8 Functional interface "Supplier" as the parameters, each supplier evaluates to a Boolean.

 Boolean and(Supplier<Boolean> x, Supplier<Boolean> y) {
  if (x.get()) return y.get();
  else return Boolean.FALSE;

and finally the call to "and":

and(() -> false, this::loop)

The call uses Lambda expressions, the first "Supplier" simply returning false, the second Supplier a call to loop using method references. In this case, like with Scala, the call does not blow up as the second parameter is only evaluated when the first expression is true - since the call is with the first expression being false, it evaluates cleanly to "false".

Again the Java 8 equivalent is not as elegant as Scala, but works well enough.

No comments:

Post a Comment