Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What did Java programmers do before Java 8 if they wanted to pass a function around?

Please be gentle with me.

I hear Java 8 introduced lambdas. But before that, if you wanted to pass around a function, say, as an argument, what did you do?

One way I can think of is to create a single method interface like so:

public interface ISingleMethodInterface
{
  bool Really(int n);
}

public bool GimmeFunction(ISingleMethodInterface interface, int n)
{
  return interface.Really(n);
}

But that is a very limited application of functions as first class citizens because:

  1. You cannot do much other than to execute that function or to pass that object around to another method. With lambdas, you can compose. For e.g. you can negate that lambda like so:

    public bool GimmeLambdaAndIWillComputeItsInverse(Func<int, bool> predicate, int n)
    {
      return !predicate(n);
    }
    
  2. You cannot return lambdas or their derivatives. I mean, you can only return that same object like so:

    // I know Java doesn't have Tuples but let's forget that for now
    public Tuple GimmeFunction(ISingleMethodInterface interface, int n)
    {
      return new Tuple { Item1 = interface, Item2 = n };
    }
    

    With lambdas, you can return derivatives like so:

    public Func<int, bool> GetInverseFunction(Func<int, bool> predicate, int n)
    {
      return n => !predicate(n);
    }
    

So, what did you do if you needed to do anything like this?

like image 782
Water Cooler v2 Avatar asked Jul 16 '16 23:07

Water Cooler v2


People also ask

What was introduced with Java 8?

Java 8 has introduced forEach method in java. lang. Iterable interface so that while writing code we focus on business logic. The forEach method takes java.

Why do we need functional interfaces in Java 8?

Functional interfaces are included in Java SE 8 with Lambda expressions and Method references in order to make code more readable, clean, and straightforward. Functional interfaces are interfaces that ensure that they include precisely only one abstract method.

What is the functional interface in Java 8?

An Interface that contains exactly one abstract method is known as functional interface. It can have any number of default, static methods but can contain only one abstract method. It can also declare methods of object class. Functional Interface is also known as Single Abstract Method Interfaces or SAM Interfaces.

Which is not introduced in Java 8?

1. Which of the following is not introduced with Java 8? Explanation: Serialization is not introduced with Java 8.


1 Answers

One simply passed instances of functional interfaces. Your denigration of this relative to lambdas is a little off because in Java, lambdas are literally functional interfaces. So since one is implemented in terms of the other, they are neither stronger than the other. The difference is in how the source readability strikes you.

like image 68
Lew Bloch Avatar answered Oct 04 '22 13:10

Lew Bloch