Answers.Body: <p>In this case, what you're doing is expressed in a slightly clearer way in ruby:</p>
  
  <pre><code>someList.each do |element|
    print element
  end
  </code></pre>
  
  <p>What you're doing is passing <code>forEach</code> a block which it is to run on every element of <code>someList</code>, and to run the block on every element with the element stored in the variable <code>l</code>.</p>
  
  <p>Here is a way that this could have been expressed, which could add clarity:</p>
  
  <pre><code>for (i=0;i &lt; someList.length;i++) {
    l = someList[i];
    System.out.println(l+"");
  }
  </code></pre>
  
  <p>Another possibility using <a href="https://stackoverflow.com/q/7763131/1544337">Java's for-each</a> is:</p>
  
  <pre><code>for (Elem l : someList) {
    System.out.println(l + "");
  }
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-06-13T16:44:45.333Z
Answers.LastActivityDate: 2017-06-13T19:37:35.863Z
Answers.LastEditDate: 2017-06-13T19:37:35.863Z
Answers.UserDisplayName: thesecretmaster
Answers.Body: <p>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.</p>
  
  <p>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.  </p>
  
  <p>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.</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-06-13T22:09:13.713Z
Answers.LastActivityDate: 2017-06-13T22:09:13.713Z
Answers.UserDisplayName: ncmathsadist
Body: <p>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.</p>
  
  <p>For example:</p>
  
  <pre><code>String firstString = "this is a lambda example!";
  String secondString = "and we compare to this, as well";
  Function&lt;String, Double&gt; comparison = (String third) -&gt;
          (third.compareTo(firstString) + third.compareTo(secondString)) / 2.0;
  </code></pre>
  
  <p>would create a function that gives the average of the comparison between a given string and <code>firstString</code> and <code>secondString</code>. 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.</p>
  
  <p>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? </p>
  
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2017-06-13T17:12:12.903Z
Comments.Text: 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.
Comments.UserDisplayName: Gypsy Spellweaver
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2017-06-13T18:05:20.877Z
Comments.Text: @GypsySpellweaver a scalar?
Comments.UserDisplayName: ItamarG3
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2017-06-13T18:16:21.787Z
Comments.Text: Single item not a list or structure, such as int double chat etc.
Comments.UserDisplayName: Gypsy Spellweaver
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2017-06-13T18:29:12.287Z
Comments.Text: 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.
Comments.UserDisplayName: ctrl-alt-delor
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2017-06-19T06:42:18.930Z
Comments.Text: "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`](http://download.java.net/java/jdk9/docs/api/java/lang/invoke/package-summary.html), `invokedynamic`, `MethodHandle`, bootstrap methods, `MethodType`, and the [`LambdaMetafactory`](http://download.java.net/java/jdk9/docs/api/java/lang/invoke/LambdaMetafactory.html) 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.
Comments.UserDisplayName: Jörg W Mittag
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2017-06-19T06:44:57.267Z
Comments.Text: @JörgWMittag Clever tricks for performance is key. I want students to learn why they're useful. And that is part of it.
Comments.UserDisplayName: ItamarG3
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2017-06-19T06:48:35.567Z
Comments.Text: Sure, but the whole machinery is probably going to take a long time to explain, unless the students are already intimately familiar with JSR-292, `invokedynamic`, bootstrap methods, `MethodHandle`s and so on. I certainly don't know how it works, I freely admit. It *really* is a very clever machinery with lots of moving interrelated parts. (Or maybe I am wrong and it is totally simple, I just haven't seen it yet. Feel free to correct me.)
Comments.UserDisplayName: Jörg W Mittag
Tags: <lambda-expressions>
Title: How to explain lambda expressions
ThreadText: # How to explain lambda expressions
  
  (Asked by: username_0 on 2017-06-13)
  
  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?
  
  **Tags:** lambda-expressions
  
  > **Comments:**
  > **username_1**: 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.  
  > **username_0**: @username_1 a scalar?  
  > **username_1**: Single item not a list or structure, such as int double chat etc.  
  > **username_2**: 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.  
  > **username_3**: "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`](http://download.java.net/java/jdk9/docs/api/java/lang/invoke/package-summary.html), `invokedynamic`, `MethodHandle`, bootstrap methods, `MethodType`, and the [`LambdaMetafactory`](http://download.java.net/java/jdk9/docs/api/java/lang/invoke/LambdaMetafactory.html) 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.  
  > **username_0**: @username_3 Clever tricks for performance is key. I want students to learn why they're useful. And that is part of it.  
  > **username_3**: Sure, but the whole machinery is probably going to take a long time to explain, unless the students are already intimately familiar with JSR-292, `invokedynamic`, bootstrap methods, `MethodHandle`s and so on. I certainly don't know how it works, I freely admit. It *really* is a very clever machinery with lots of moving interrelated parts. (Or maybe I am wrong and it is totally simple, I just haven't seen it yet. Feel free to correct me.)  
  
  ---
  
  ## 2 answers
  
  ### Answer by username_5 on 2017-06-13. Score: 5 | Accepted answer
  
  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.
  
  
  ---
  
  ### Answer by username_4 on 2017-06-13. Score: 0
  
  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](https://stackoverflow.com/q/7763131/1544337) is:
  
  ```
  for (Elem l : someList) {
    System.out.println(l + "");
  }
  
  ```
  
  
  ---
  
  