8
$\begingroup$

This year I will teach students about lambda expressions (in java). I am trying to explain how they are interpreted by the JVM and what they unfold into.

For example:

String firstString = "this is a lambda example!";
String secondString = "and we compare to this, as well";
Function<String, Double> comparison = (String third) ->
        (third.compareTo(firstString) + third.compareTo(secondString)) / 2.0;

would create a function that gives the average of the comparison between a given string and firstString and secondString. I've tried to explain to a selection of students (as a Proof of Concept of my planned lesson, so only 4-5 students) that lambda expressions just make things look better but they think that it's a new way to do this. I am trying to explain that lambda expressions are simply a shortened way to write something a bit bigger.

So my question is, how do I explain what lambda expressions exactly are, in such a way that shows the students how these expressions are interpreted by Java?

$\endgroup$
  • 1
    $\begingroup$ To make things clearer, try using a scalar rather than a list. The implicit loop of forEach muddies the water. And, maybe, try using an assignment rather than printing inside the lambda expression. KISS principle. $\endgroup$ – Gypsy Spellweaver Jun 13 '17 at 17:12
  • $\begingroup$ @GypsySpellweaver a scalar? $\endgroup$ – ItamarG3 Jun 13 '17 at 18:05
  • $\begingroup$ Single item not a list or structure, such as int double chat etc. $\endgroup$ – Gypsy Spellweaver Jun 13 '17 at 18:16
  • $\begingroup$ In any OO language you can implement lambda expressions: first create an interface lambda_string with method doit( value:string ). Now implement foreach_string( l: list of string, L: lambda_string ). Now show how to create in instance of lambda_string. One you have that done, do it again with templates/generics. Now explain that l -> {…} is syntactic sugar for what you just did. $\endgroup$ – ctrl-alt-delor Jun 13 '17 at 18:29
  • $\begingroup$ "I am trying to explain how they are interpreted by the JVM and what they unfold into." – Why? I mean, I get that the whole machinery using java.lang.invoke, invokedynamic, MethodHandle, bootstrap methods, MethodType, and the LambdaMetafactory is very interesting, but I don't think it is a good thing to teach. It is, after all, full of implementation details and clever tricks for performance. $\endgroup$ – Jörg W Mittag Jun 19 '17 at 6:42
5
$\begingroup$

A lambda is an anonymous function. Java handles lambdas via a feat of type inference. What you have done is to assign a lambda to a variable of interface type. You may only do this if the interface is a functional interface, i.e., it is an interface that specifies exactly one [non-defaullt] method.

When you do this assignment, the compiler assumes that the lambda is the method specified by the interface that is they type of the variable. It is here that type inference occurs. An object of unnamed type implementing the interface is crated and your variable now points at it. The compiler will reject your code if the argument list or return type of the lambda is incompatible with the interface.

You will find lambdas to be splendid if you are doing event-driven programming. In fact the JavaFX framework takes big advantage of this feature.

$\endgroup$
1
$\begingroup$

In this case, what you're doing is expressed in a slightly clearer way in ruby:

someList.each do |element|
  print element
end

What you're doing is passing forEach a block which it is to run on every element of someList, and to run the block on every element with the element stored in the variable l.

Here is a way that this could have been expressed, which could add clarity:

for (i=0;i < someList.length;i++) {
  l = someList[i];
  System.out.println(l+"");
}

Another possibility using Java's for-each is:

for (Elem l : someList) {
  System.out.println(l + "");
}
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.