• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Rob Spoor
  • Devaka Cooray
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Jj Roberts
  • Al Hobbs
  • Piet Souris

Lambda question for Peter Verhas

 
Ranch Hand
Posts: 574
VI Editor Chrome Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How well do you think you explain lambdas?  I've gotten to where I understand them, but don't see the need.  There always seems to be a better way to solve the problem.

Are they going to be like Associative Arrays (I learned them in Awk), where you understand them but wonder "who would ever need that?".  Then you have to parse log files based on an alpha-numeric field and the light bulb goes on.  See also, Posix writev() and readv(), used them for a wireless driver.  Intercepted outgoing ethernet packets, slapped my header in front of it via writev().  Received wireless packets got the header stripped via readv() and passed to the system like a normal Ethernet packet.

2 things I knew about for years but could never understand a need for.
 
Marshal
Posts: 74348
334
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jim Venolia wrote:How well do you think you explain lambdas?

Who is explaining them? Peter Verhas?

I've gotten to where I understand them, but don't see the need. . . .

Well, people managed to program for about 18 years without λs being available, but complained about the verbosity of anonymous classes.
 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The purpose is just to reduce verbosity. Java is a quite old language.
But with verbosity reduction also comes with higher difficulty to read, especially for people who are new to lambda concept.

Thank You,
Ed
 
Campbell Ritchie
Marshal
Posts: 74348
334
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
EF, welcome to the Ranch
 
Marshal
Posts: 16631
278
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Lambdas also get you closer to being able to do functional programming in Java. That was a big point of envy with other languages like Scala and even JavaScript.

A lambda in Java is basically an anonymous implementation of a functional interface, an interface that has only one method. So, wherever you declare some kind of functional interface as a parameter, you can pass in a lambda expression that is compatible with the functional interface's single method signature.

I find that quite useful, actually, and it allows you to write more expressive and intent-focused code.
 
Saloon Keeper
Posts: 13369
295
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Eddy Fang wrote:The purpose is just to reduce verbosity. Java is a quite old language.
But with verbosity reduction also comes with higher difficulty to read, especially for people who are new to lambda concept.


Patently untrue.

The purpose is to write declarative code. Why do we have languages like HTML to describe what we want web pages to look like, when we could also have designed an imperative language that tells the browser to draw a line here now, then draw some text there, etc.?

The only reason lambdas are difficult for beginners is that they're only used to imperative programming. For a programmer who started out with Haskell, telling the computer what to do one statement at a time would be a very difficult concept. Indeed, many mathematicians have a hard time with Java and C++.
 
Bartender
Posts: 4670
183
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Stephan van Hulst wrote:

Eddy Fang wrote:The purpose is just to reduce verbosity. Java is a quite old language.
But with verbosity reduction also comes with higher difficulty to read, especially for people who are new to lambda concept.


Patently untrue.


Why? As others have idicated, a lambda is just a convenient shorthand for writing an anonymous class: whatever your lambda, you could achieve the same thing in java <8. So, I agree with Eddy on this point.

I don't agree with lambda's being difficult. What is difficult is the other major new thing in java 8: Streams and consecutives. Looking at the API of, say, Collectors will scare you off for months.

Stephan van Hulst wrote:The purpose is to write declarative code. Why do we have languages like HTML to describe what we want web pages to look like, when we could also have designed an imperative language that tells the browser to draw a line here now, then draw some text there, etc.?


Can yuo give some examples that are declarative and the equivalent imparative way? I have some doubts about this, so a good example can make this subject a little clearer for me. Thanks upfront.
 
Stephan van Hulst
Saloon Keeper
Posts: 13369
295
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Piet Souris wrote:Why? As others have idicated, a lambda is just a convenient shorthand for writing an anonymous class: whatever your lambda, you could achieve the same thing in java <8. So, I agree with Eddy on this point.


The opportunities for writing an anonymous type were very few before Java 8. I only ever wrote them for listeners in UIs, and even then I would mostly write an inner class.

I don't agree with lambda's being difficult. What is difficult is the other major new thing in java 8: Streams and consecutives. Looking at the API of, say, Collectors will scare you off for months.


It's precisely the Streams API that made lambdas necessary. The Streams API is intended to make code more declarative, and without lambdas it would be way too unwieldy to adopt. Lambdas are the natural language of functional programming.

And again, the Collectors API is not crazy difficult if you have a functional background. That's why I always advocate learning functional programming from the get go, as opposed to as an afterthought after people have learned the so called 'basics' of Java.

Can yuo give some examples that are declarative and the equivalent imparative way? I have some doubts about this, so a good example can make this subject a little clearer for me. Thanks upfront.


The simplest example I can think of is transforming a collection of objects and then connecting the results in a list.

Old way:

New way:

In the old code, we had to make several choices we don't care about: What list implementation do we want to collect our results in? How do we collect our results into the list? Do we process our input sequentially or parallel?

The new code stated exactly what we wanted to achieve and nothing more. We want to transform all the input and then collect it to a list. We didn't say what list. We didn't say that that the add() method needs to be called repeatedly. We didn't say that the process can't happen concurrently.

Heck, maybe in the future we'll have computers that can magically populate a list without memory footprint in zero execution time. I won't have to change my new code, because I only told the computer what I wanted to achieve, not how I wanted to achieve it.
 
Piet Souris
Bartender
Posts: 4670
183
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi Stephan,

thanks. I expected an example like this, and my problem was that the distinction between the 'what' and the 'how' is to a certain degree arbitrair. In your stream example, I can say that I also tell java how to do something: first, create a stream, then map the elements to something else, and then put these in a list. In the old way, I specify these things too, but in such a way that it looks totally different, and perhaps I supply some more details.
Now, I'm very happy with what you call the declarative way. It makes programming just that much easier, after some practising.
 
Stephan van Hulst
Saloon Keeper
Posts: 13369
295
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I understand that the distinction may seem arbitrary, and the point I'm about to make may appear as if I'm fixing the game by defining things to my advantage.

Streams ARE the line that separate the what and the how.

The problem is that Java is not a functional language period. The primary mechanism through which pure functional languages achieve their "functional-ness" is lazy evaluation. Lazy evaluation should be a built-in language feature, something that Java tries to emulate through Streams. So yes, in a way you're correct that we're still performing procedural programming by saying we first perform this operation on a stream, and then we perform that operation on a stream, etc. There are two flaws with this reasoning: Using streams is not the real goal. They're just a tool that help us write down the 'what' and not the 'how'. Secondly, method invocation chaining, whether on streams or not, is not the thing that makes a snippet of code procedural. Executing anything else than a single return statement is what makes a snippet of code procedural. Chaining two methods calls together just defines an order of operations that a transformation consists of. If laziness is built into the language, method invocation chaining doesn't actually do anything but function composition, something that can be done compile time. There is no time component that's added run-time. Executing two statements, one after the other, introduces a time component. Execute this statement, and when it's done, execute the next.

If it helps, think of Java as a lazily evaluated language where return types are implicitly streams, and where the Stream class itself was only added for the benefit of reflection (much like we have Class, Method, Array, etc.). The methods of Stream would then be operators much like instanceof is an operator. My earlier code snippet would then look like this, if we used infix notation:

I think I've said it before, but I really recommend that every programmer learns a few languages of wildly different paradigms. Invest some time in learning Haskell or Erlang, and stuff like this will become much more natural.
 
Ranch Hand
Posts: 393
9
Open BSD BSD Debian
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Stephan van Hulst wrote:... That's why I always advocate learning functional programming from the get go, as opposed to as an afterthought after people have learned the so called 'basics' of Java....



Thumps up
 
Stephan van Hulst
Saloon Keeper
Posts: 13369
295
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Harry, what does your link have to do with the topic?
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic