• 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
  • Devaka Cooray
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Jeanne Boyarsky
  • Tim Cooke
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Mikalai Zaikin
  • Carey Brown
Bartenders:

Lambda fuctions in Python as compared to Java Lambda function

 
Saloon Keeper
Posts: 15276
350
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why don't you show us an example of the two alternatives, and then tell us what you think the pros and cons of the two approaches are?
 
Ranch Foreman
Posts: 2891
13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Stephan van Hulst wrote:Why don't you show us an example of the two alternatives, and then tell us what you think the pros and cons of the two approaches are?



Sure, below are the examples of lambda to add two numbers in Java and Python and also the code of doing it in way other than lambda.

Java :




Python




What I can see is:


Advantage of lambda in Java over doing the same thing without lambda :

Without lambda for this we required anonymous class implementation code, which made the code less readable. With lambda it is more readable.


Advantage of lambda in Python over way of doing it without lambda :

Only that one does not have to create a function.

Can one say that using lambda gives more advantage over not using it in Java as compared to what it gives in Python ?
 
Stephan van Hulst
Saloon Keeper
Posts: 15276
350
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You're not really using the lambda expressions correctly though. The point of a lambda expression is to pass the function it evaluates to to a higher order function, not to call it directly. Here is a better example:

Without lambda expressions, Python:

With lambda expressions, Python:

As you can see, lambda expressions in Python allow you to define a function inline with the higher order function call. This makes the code slightly less verbose, but the tradeoff is that your code becomes slightly more difficult to read.

Personally, I find that lambda expressions are rarely worth it. I prefer the more readable version where you declare a named function.

In Java, I would declare an isEven() method explicitly, and then pass it as a predicate function to the filter() call by using a method reference:
 
The only taste of success some people get is to take a bite out of you. Or this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic