• 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
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

Lambda fuctions in Python as compared to Java Lambda function

 
Bartender
Posts: 15737
368
  • 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 Hand
Posts: 2957
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
Bartender
Posts: 15737
368
  • 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:
 
Monica Shiralkar
Ranch Hand
Posts: 2957
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: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.



Sorry, I can see that in my example I focused on method in functional interface and missed the real thing that is passing to the higher order functions.

I think the below is what I could have done instead of that




 
My, my, aren't you a big fella. Here, have a tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic