Please be careful with me.
I heard that Java 8 introduced lambda. But before that, if you want to pass a function, say, as an argument, what did you do?
One way I can imagine is to create a single method interface as follows:
public interface ISingleMethodInterface
{
bool Really(int n);
}
public bool GimmeFunction(ISingleMethodInterface interface, int n)
{
return interface.Really(n);
}
But this is a very limited use of functions as first-class citizens, because:
You can do nothing but execute this function or pass this object to another method. With lambdas you can compose. E.g. you can undo this lambda like this:
public bool GimmeLambdaAndIWillComputeItsInverse(Func<int, bool> predicate, int n)
{
return !predicate(n);
}
You cannot return lambda or its derivatives. I mean, you can return only the same object:
public Tuple GimmeFunction(ISingleMethodInterface interface, int n)
{
return new Tuple { Item1 = interface, Item2 = n };
}
With lambdas, you can return derivatives like this:
public Func<int, bool> GetInverseFunction(Func<int, bool> predicate, int n)
{
return n => !predicate(n);
}
, , - ?