1
\$\begingroup\$

Preface

This question is specifically targeting Java answers. I have little to no knowledge of any other program languages, though I expect that the questions I raise below will apply to other statically-typed languages as well. This question was "inspired" by this PPCG answer.

Java Lambdas

(skip this if you know Java and how Java lambda's work)

So, in statically typed languages, everything has a type at compile-time - including lambdas. In Java lambdas are all instances of some interface which is considered a FunctionalInterface. An interface is functional iff it contains only 1 non-default method. Lambdas for that interface can then be created implementing that 1 method. For example, below is the (simplified) source code for Java's Function class:

public interface Function<T, R> {

    R apply(T t);

}

Lambdas of the type Function<T, R> can then be implemented like: t -> r, where t is the input and r is the return value. Function is just one of the many functional interfaces Java provides, others include Predicate, Consumer, Supplier, BiFunction, etc. In addition, interfaces which already existed but, as of Java 8, fit the definition of functional interfaces (such as Runnable) can also be implemented as lambdas.

The Question

Every answer posted as a Java lambda that I've seen (up until the one that inspired this question) uses one of the default Java types. I myself always specify what type the lambda is, to avoid any confusion. The question I have now is, can one just arbitrarily write a lambda, with as many parameters required, and submit it, as is done in this answer (and claim it implements a custom functional interface)?

Can lambdas be submitted as answers despite requiring an additional class declaration?


Notes

  • I do not intend to specifically call out Okx's answer; it just so happens to be the one that spurred me to write this question. As far as I can tell, no community consensus was reached on this issue before they posted it, so don't go flooding their answer with down votes...
  • Of course, you could also just use a certain lambda library I may or may not have created and side step this whole issue... and Java isn't going to win any challenges anyway... this kind of feels like a pointless question now... Anyway, thoughts?
  • If this question is a duplicate, I apologize, I couldn't find any other similar questions with the search term: lambda AND class is:question
\$\endgroup\$

2 Answers 2

6
\$\begingroup\$

Yes

  • All well-specified questions define their inputs and the outputs. This is effectively defining your interface. Requiring it is like requiring the code the runs your submission.
  • All lambdas that require primitive types (unboxing doesn't work in their case) would need to declare their interface
  • If you really want to get technical, EasyBind has functional interfaces for up to 6 inputs, and I would simply need to indicate in my submission that it implements that interface. (I don't really like this, but it is the natural extension of this meta post)
\$\endgroup\$
4
  • \$\begingroup\$ So the question is.the interface? That actually makes a lot of sense... \$\endgroup\$ Jun 23, 2017 at 12:11
  • \$\begingroup\$ Formatting was messed up in my question! In the notes I tried to mention that InverseY provided functional interfaces for as many 0-255 parameters... but the link pointed to the wrong thing... anyway, this answer makes a lot of sense to me, so I'll accept it :) \$\endgroup\$ Jun 23, 2017 at 13:37
  • 1
    \$\begingroup\$ @SocraticPhoenix don't accept it. Let the community decide what it wants. \$\endgroup\$ Jun 23, 2017 at 16:34
  • \$\begingroup\$ I appear to have forgotten to accept it anyway... but so be it :) \$\endgroup\$ Jun 23, 2017 at 16:48
-2
\$\begingroup\$

No.

Lambdas must be of a built-in type (such as Function in Java).

The key distinction, in my eyes, is boilerplate. With a built-in Java lambdas, one only needs the normal code required to get a full, compilable program:

import java.util.function.BinaryOperator;

public class Test {

    public static void main(String[] args) {
        BinaryOperator<Integer> add = (a, b) -> a + b;
    }

}

However, a custom lambda type requires a full class declaration, as well as boilerplate, to make it compilable:

public class Test {

    public static void main(String[] args) {
        TriFunction<Integer, Integer, Integer, Integer> addThree = (a, b, c) -> a + b + c;
    }
    
    public interface TriFunction<A, B, C, R> {
        
        R apply(A a, B b, C c);
        
    }

}

in my opinion, defining a whole new type that is than used in the program (as opposed to a class created only to house the main method) simply cannot be considered boilerplate.

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .