This week's book giveaway is in the NodeJS forum.
We're giving away four copies of Serverless Applications with Node.js and have Slobodan Stojanovic & Aleksandar Simovic on-line!
See this thread for details.
Win a copy of Serverless Applications with Node.js this week in the NodeJS forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Junilu Lacar
  • Paul Clapham
  • Knute Snortum
Saloon Keepers:
  • Stephan van Hulst
  • Ron McLeod
  • Tim Moores
  • salvin francis
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Vijitha Kumara

Lambdas - A contrived example for the Java rookies  RSS feed

 
Ranch Hand
Posts: 99
Java Linux Monad
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I decided to post a contrived lambda example to show the Java rookies what can be achieved with this simple idea. Basically I'm going to use lambdas and closures to build a simple data structure that I can store and retrieve data. Remember, this data structure is a function that I build as the user added data.

The first thing we need is a single method interface to define our lambda. This lambda function will take a key and return an optional value;



Next we need another lambda to handle comparing data passed to our lambda data structure...



Now we need to create the data structure or dictionary. The dictionary will build and store a lambda functions that we can fetch values from.




A short example of usage.


This posting was not a tutorial on lambdas, this posting was demonstrating the mind blowing concepts that are possible with lambdas and closures. If you come away from this post shaking your head in bewilderment... Then the post did its job.


 
Marshal
Posts: 63843
209
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Added to our λs forum.

Afraid that code is illegible because you have such poorly‑chosen variable names.
 
Ranch Hand
Posts: 139
5
MS IE Notepad Suse
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also: you prefixed one of your Interfaces with a capital I to construct a name wich tells its type.

Two hints:
1) Although it's not prohibeted, but prefixing interfaces with a capital I just isn't Java. We have our conventions, and it's adviced to use them. Espacialy if you write source as example wich is to be read by other devs.
2) Do one thing, and do it always: either prefix every interface or none at all - but don't mix different styles within the same project.

Javas conventions suggest classnames (yes, this also applies to interfaces) to be "speaking nouns" so a classname "tells" what the class does or what it is used for. IHyfuncs and dict(), as well as the package name HyFuncs (packages should be all lower case), are very poorly choosen. Sure, naming doesn't affect functionality, but it makes it harder to understand the code.

Sorry to be the grammar nazi again, but as lambdas and functional programming is advanced topic - the basics should be done right.
 
Campbell Ritchie
Marshal
Posts: 63843
209
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am not convinced that λs are an advanced topic myself.
 
Sheriff
Posts: 24295
55
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:I am not convinced that λs are an advanced topic myself.



No, but for rookies who have been raised on a diet of arrays and basic for-loops and data read from the console, it's something completely different.
 
Sheriff
Posts: 13391
221
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Gerard Gauthier wrote:I decided to post a contrived lambda example to show the Java rookies what can be achieved with this simple idea.
...
This posting was not a tutorial on lambdas, this posting was demonstrating the mind blowing concepts that are possible with lambdas and closures. If you come away from this post shaking your head in bewilderment... Then the post did its job.


I know quite a bit about lambdas and closures and I'm pretty sure that what you refer to as a "single method interface" is officially called a "functional interface." You also refer to the EqualTo functional interface as a lambda, which it isn't.

I'm also shaking my head in bewilderment, not because I don't understand lambdas and closures but because I fail to see how people coming away from this post shaking their heads in bewilderment achieves your stated goal of showing Java rookies what can be achieved by this simple idea. Unless, of course, if the "what can be achieved" that you had in mind was that this simple idea can be used to create terribly convoluted code that even experienced people like Campbell would have a hard time understanding...

Martin Fowler wrote:Any fool can write code that a computer can understand. Good programmers write code that humans can understand.



Lambda expressions were meant to provide a better alternative to anonymous classes in Java. They are quite powerful but with much power also comes much responsibility. In my opinion, your example shows how that power and responsibility can be easily abused. It's a pretty good example of how you shouldn't program with lambdas. If that was your actual goal then, yeah, I think your post did its job.
 
Gerard Gauthier
Ranch Hand
Posts: 99
Java Linux Monad
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think you missed the point, even when that point was mentioned in the post.

It wasn't meant to be a practical guide to lambdas. It was showing off the lambda's potential.

Basically lambda's are a nice convenience for the everyday programmer but their features are rarely explored or used to the extreme.

The difference between a good programmer and a exceptional programmer is, the exceptional programmer knows where the boundaries are.  
 
Campbell Ritchie
Marshal
Posts: 63843
209
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Junilu Lacar wrote:. . . even experienced people like Campbell would have a hard time understanding... . . .

I got as far as thinking, “That's an awkward way to create a linked list,” and gave up. The dreadful variable naming doesn't help. of course.
 
Junilu Lacar
Sheriff
Posts: 13391
221
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Gerard Gauthier wrote:I think you missed the point, even when that point was mentioned in the post.
...
The difference between a good programmer and a exceptional programmer is, the exceptional programmer knows where the boundaries are.



Did you mean the stated point of "demonstrating the mind blowing concepts that are possible with lambdas and closures" or the implied point of being an exceptional programmer? Because I still disagree that you demonstrated either of them. Sorry.
 
Junilu Lacar
Sheriff
Posts: 13391
221
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here's the thing: it rubs me (and probably a few other people) the wrong way when you post something like that in a beginner forum and basically say, "Here's an example of what an awesome programmer like me can do with lambdas and closures and if you come away in awe of what I did even if you don't understand it, it's because I know what this code is doing and you still have a long way to go before you will."

If that wasn't your intent, it sure comes across like it. If you had posted this in our other forum about Puzzles and programming diversions, it would have come across totally differently and we would be having a different kind of conversation.
 
Junilu Lacar
Sheriff
Posts: 13391
221
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Gerard Gauthier wrote: lambda's are a nice convenience for the everyday programmer but their features are rarely explored or used to the extreme.


I can only agree to the second half of this statement. Yes, they are underutilized and not enough programmers are leveraging their power.

However, lambdas are more than just "nice conveniences." That kind of implies that they are syntactic candy, like the for-each loop. In my opinion, lambdas are more than that. They are an attempt to facilitate functional-style programming constructs in Java. That requires a paradigm shift in the way you approach problem solving. Perhaps that is why or at least partly the reason why not that many programmers have adopted them them in their daily work.
 
Gerard Gauthier
Ranch Hand
Posts: 99
Java Linux Monad
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Junilu Lacar wrote:

Gerard Gauthier wrote: lambda's are a nice convenience for the everyday programmer but their features are rarely explored or used to the extreme.


I can only agree to the second half of this statement. Yes, they are underutilized and not enough programmers are leveraging their power.

However, lambdas are more than just "nice conveniences." That kind of implies that they are syntactic candy, like the for-each loop. In my opinion, lambdas are more than that. They are an attempt to facilitate functional-style programming constructs in Java. That requires a paradigm shift in the way you approach problem solving. Perhaps that is why or at least partly the reason why not that many programmers have adopted them them in their daily work.



I completely agree. Lambdas and closures open a whole new way of formulating solutions. One just has to look at Java's Stream library and other related libraries to see the influence of  functional programming.
 
Junilu Lacar
Sheriff
Posts: 13391
221
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And I do apologize for jumping down your throat a little bit there. I should have given you the benefit of the doubt and maybe IM'd you first about your intent. I did not give you the same deference someone else gave me for a snarky post recently and I failed to pay that forward to you. No hard feelings, I hope but please, going forward, also consider your audience and the skill-appropriateness of any examples you wish to share. We're here to help each other learn and if nobody can understand the example, then there's no learning.
 
Gerard Gauthier
Ranch Hand
Posts: 99
Java Linux Monad
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Junilu Lacar wrote:And I do apologize for jumping down your throat a little bit there. I should have given you the benefit of the doubt and maybe IM'd you first about your intent. I did not give you the same deference someone else gave me for a snarky post recently and I failed to pay that forward to you. No hard feelings, I hope but please, going forward, also consider your audience and the skill-appropriateness of any examples you wish to share. We're here to help each other learn and if nobody can understand the example, then there's no learning.



No problem. I don't mind confrontation, I actually think it helps in some cases.

The example was to 'hopefully' spark some curiosity. Whenever I see some code that I can't understand, I immediately start pondering on what's happening and why. Not knowing has pushed me into some every obscure corners of programming.
 
Campbell Ritchie
Marshal
Posts: 63843
209
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Junilu Lacar wrote:And I do apologize . . . .


Gerard Gauthier wrote:. . . . I don't mind confrontation . . .

Well done, both of you, sorting out the confrontation
 
I promise I will be the best, most loyal friend ever! All for this tiny ad:
global solutions you can do in your home or backyard
https://coderanch.com/t/708587/global-solutions-home-backyard
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!