• 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

 
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
In Java Lambda functions are functions that do not belong to any class. In Python, class is not mandatory. So, what are lambda functions in Python? Thanks
 
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
They are not called "lambda functions". They are called "lambda expressions".

In Java, a lambda expression is an expression that when evaluated, returns an object of a functional type.

In Python, a lambda expression is an expression that when evaluated, returns a function object.

The difference is that functions are first class citizens in Python. Java doesn't have function objects. You can not "invoke an object" in Java.
 
Monica Shiralkar
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
Thanks. So whether it's Java or Python, I think we can say in simple way that lambda expressions are like functions/methods which do something, may return something but does not have a name.

In terms of functional programming, in Java we can say that lambda expressions do not belong to any class. We can't say the same in Python as class isn't mandatory there.
 
Saloon Keeper
Posts: 27494
195
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Lambas are a core concept in the LISP programming language. In LISP, a Lamba is a list whose components define parameters and an expression to be evaluated using those parameters.

And in LISP, a function is a Lambda which has been mapped to a name in the workspace dictionary allowing it to be located and invoked by looking up that name. Thus, by definition, LISP lambdas are always expressions, not functions.
 
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

Monica Shiralkar wrote:I think we can say in simple way that lambda expressions are like functions/methods which do something, may return something but does not have a name.


NO. Lambda expressions are NOT functions. They are expressions.

Consider the following analogy:

The bit of code "1 + 2" is NOT an integer. It is an expression that, when evaluated, returns an integer.

Similarly, the bit of code "person -> person.getName()" is NOT a function. It is an expression that, when evaluated, returns a function.

In terms of functional programming, in Java we can say that lambda expressions do not belong to any class. We can't say the same in Python as class isn't mandatory there.


This makes no sense.

In terms of functional programming, there is no such thing as a "class".
 
Monica Shiralkar
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:
NO. Lambda expressions are NOT functions. They are expressions.


I did not say that lambda expressions are functions and only said that they are like functions, in the sense that like functions may take some input and return an output, lambda expressions may take some input and return an output.


Stephan van Hulst wrote:
In terms of functional programming, there is no such thing as a "class".


Thanks. Understood.
 
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
Again, no.

Expressions don't take any input. They only evaluate to some value.
 
Monica Shiralkar
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:They are not called "lambda functions". They are called "lambda expressions".



But what the below link says is that there are lambda functions (which are small annomous functions )which have lambda expressions in it :
[Link] https://www.w3schools.com/python/python_lambda.asp[/Link]
 
Stephan van Hulst
Saloon Keeper
Posts: 15276
350
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
W3Schools is not an authoritative source. They typically employ colloquial or informal terms that may be good enough for a simple tutorial, but which aren't strictly correct.
 
Monica Shiralkar
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:W3Schools is not an authoritative source. They typically employ colloquial or informal terms that may be good enough for a simple tutorial, but which aren't strictly correct.



Ok but other websites like [link].      https://realpython.com/python-lambda/ [/link]are also using the term "lambda functions".
 
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
This will go a lot faster if you accept that 99.9% of all websites contain many factual inaccuracies.

You realize that most people that write tutorials on the internet have learned a language by reading other tutorials on the internet, yes?
 
Monica Shiralkar
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:This will go a lot faster if you accept that 99.9% of all websites contain many factual inaccuracies.

You realize that most people that write tutorials on the internet have learned a language by reading other tutorials on the internet, yes?



Ok. So means there is nothing called "lambda functions'  in Python functional programming and only thing in Python functional programming is lambda expressions ? And the purpose of lambda expressions is to get rid of the need for annomous functions?
 
Marshal
Posts: 28009
94
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Stephan van Hulst wrote:This will go a lot faster if you accept that 99.9% of all websites contain many factual inaccuracies.

You realize that most people that write tutorials on the internet have learned a language by reading other tutorials on the internet, yes?



I just did a quick survey of the entire internet and my impression is that "lambda function" tends to lead to articles about Python whereas "lambda expression" tends to lead to articles about Java. (Other languages are scattered in there apparently randomly.)

The term "lambda" came from a mathematical system developed in the 1930's. The basic operation (I'm summarizing from Wikipedia's "Lambda calculus" article) is to take a "lambda abstraction" and bind some variables, producing a "function". So here we have an "expression" which produces a "function", just as Stephan says. This is just what we see in Java. So why Python people tend to lump the two concepts into one, I don't know.
 
Monica Shiralkar
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

Paul Clapham wrote:
my impression is that "lambda function" tends to lead to articles about Python whereas "lambda expression" tends to lead to articles about Java.



Ok. And the purpose of "lambda expressions" of Python as well as Java is to get rid of annomous functions. Correct ?
 
Stephan van Hulst
Saloon Keeper
Posts: 15276
350
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No, exactly the opposite.

The thing that a lambda expression evaluates to IS an anonymous function.
 
Tim Holloway
Saloon Keeper
Posts: 27494
195
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Stephan van Hulst wrote:No, exactly the opposite.

The thing that a lambda expression evaluates to IS an anonymous function.



Or, again, referring to the LISP terminology, a "function" is a Lambda that has been given a name. Thus.
 
Monica Shiralkar
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

Tim Holloway wrote:

Or, again, referring to the LISP terminology, a "function" is a Lambda that has been given a name. Thus.



Does that also mean that any function in Python can be converted to lambda expressions ?

What would be the equivalent lambda expressions for the below function?


Would it be like :


 
Tim Holloway
Saloon Keeper
Posts: 27494
195
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I can tell you this. That notation does, in fact both define a Lamba AND register it as a function (via its assignment to x). It won't be a first-class Python function unless you then catalog x into the Python function dictionary, which is getting a little to complex to cover here, but it is nevertheless function.
 
Monica Shiralkar
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:

The thing that a lambda expression evaluates to IS an anonymous function.


Thanks
 
Monica Shiralkar
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

Tim Holloway wrote:I can tell you this. That notation does, in fact both define a Lamba AND register it as a function (via its assignment to x). It won't be a first-class Python function unless you then catalog x into the Python function dictionary, which is getting a little to complex to cover here, but it is nevertheless function.



Thanks.
So can it be said that lambda expressions get rid of the need to explicitly write anonymous functions and when we write lambda expressions, it internally evaluates to an annomous function?

In the above example variable 'a' is required although the annomous function it translates to does not have any argument?
 
Tim Holloway
Saloon Keeper
Posts: 27494
195
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Since we have an objection to using the term "expression", perhaps the better mathematical term might be "formula". Though come to think of it, when was the last time you had to give a name or fixed values to a function (f(x)) in calculus?

Nevertheless, I wouldn't overthink it. Terminology often means whatever its principal implementers think it means, assuming that they really understand what they're saying to begin with (often not entirely true!) We have our examples of how lambdas are used in Python and how they are used in Java, and it's best to stick to the common pattern where it's reasonablt to do so, if for no other reason than not to confuse people.
 
Monica Shiralkar
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

Tim Holloway wrote:Since we have an objection to using the term "expression",



But in the above discussions it is said that it is not "lambda function" and instead "lambda expression" is what should be called.
 
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

Monica Shiralkar wrote:So can it be said that lambda expressions get rid of the need to explicitly write anonymous functions


What does that even mean? Why would you need to get rid of anonymous functions? And what do you mean by "explicitly" writing an anonymous function?

when we write lambda expressions, it internally evaluates to an annomous function?


Not sure what you mean by "internally". When an expression is executed, it evaluates to a value. In the case of a lambda expression, that value is a function.

In the above example variable 'a' is required although the annomous function it translates to does not have any argument?


Anonymous, not "annomous". And what makes you think the parameter 'a' is required? And what makes you think that the anonymous function does not have a parameter?
 
Monica Shiralkar
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 would you need to get rid of anonymous functions? And what do you mean by "explicitly" writing an anonymous function?


My understanding is that before lambda expressions came, the same thing what lambdas does was achieved by writing annomous functions.

Aren't, we getting rid of the need for writing anonymous functions?.
 
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
Then show me how you'd write an anonymous function in Python before lambdas were introduced.
 
Monica Shiralkar
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:Then show me how you'd write an anonymous function in Python before lambdas were introduced.


Yes. Sorry, I realized what I said was incorrect. What I said I think is valid for Java but not Python.

As per my understanding :

In Java - The main purpose of lambda is to avoid the need to writing anonymous methods.

In Python - This does not apply for Python and in Python lambdas itself are evaluated to annomous functions.

So what is the main purpose of lambdas in Python? I think the answer is that in Python, lambdas are used for instead of coming up with functions (with name) for everything some things are easier done by creating lambdas which get evaluated to annomous functions.

In Python, are lambdas used in 2 cases ?
1. For passing to higher order functions like map, reduce, filter.
2. Avoid the need to come up with named functions for everything and in some cases it is easier to use lambda which gets translated to anonymous functions


 
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

Monica Shiralkar wrote:In Java - The main purpose of lambda is to avoid the need to writing anonymous methods.


What's an anonymous method?

In Python, are lambdas used in 2 cases ?
1. For passing to higher order functions like map, reduce, filter.
2. Avoid the need to come up with named functions for everything and in some cases it is easier to use lambda which gets translated to anonymous functions


It seems to me are that these are the exact reasons you'd also use lambda expressions in Java.
 
Monica Shiralkar
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:
What's an anonymous method?
.



A method which does not have a name and is created on the fly.
 
Master Rancher
Posts: 4661
63
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What would that look like?
 
Monica Shiralkar
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
Sorry ,its anonymous class not anonymous methods



 
Stephan van Hulst
Saloon Keeper
Posts: 15276
350
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Still, I would not phrase it like this:

Monica Shiralkar wrote:In Java - The main purpose of lambda is to avoid the need to writing anonymous classes.



The main purpose of lambda expressions in Java is NOT to avoid the writing of anonymous classes. The main purpose of lambda expressions in Java is the same as the purpose of lambda expressions in Python, and indeed, in ANY functional language: To be able to pass anonymous functions to a higher order function.

The fact that lambda expressions in Java partially replace anonymous classes is secondary, not a main purpose.
 
Monica Shiralkar
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:
The main purpose of lambda expressions in Java is the same as the purpose of lambda expressions in Python, and indeed, in ANY functional language: To be able to pass anonymous functions to a higher order function.
.


Thanks. In case of Python these higher order functions were always part of Python and in case of Java these got added in Java 8 with the addition of streams feature. Correct ?
 
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
Not really. There were already methods that acted like higher order functions before Java 8.

For instance, the Arrays.sort(T[] a, Comparator<? super T> c) method has been around since Java 1.2. You can consider this method a higher order function, because the comparator argument is like a function.

Whether a method argument is function-like is mostly an abstract conceptual matter, not a technical matter.
 
Monica Shiralkar
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:
The main purpose of lambda expressions in Java is NOT to avoid the writing of anonymous classes. The main purpose of lambda expressions in Java is the same as the purpose of lambda expressions in Python, and indeed, in ANY functional language: To be able to pass anonymous functions to a higher order function.
.



The main purpose is to to be able to pass annomous functions to a higher order functions. Earlier, before Java 8 since lambda was not there, to higher order function, function of anonymous class was passed. So in that way is it not about avoiding anonymous class in case of passing to higher order functions ?
 
Stephan van Hulst
Saloon Keeper
Posts: 15276
350
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sure, but it's not its primary purpose.

The primary purpose of a car is not to avoid using a horse-drawn buggy.  The primary purpose of a car is to drive between two locations.
 
Monica Shiralkar
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:Sure, but it's not its primary purpose.

The primary purpose of a car is not to avoid using a horse-drawn buggy.  The primary purpose of a car is to drive between two locations.



Yes. That's a good example.

In Java, I can see examples of higher order functions are  forEach, comparator etc. For passing code to higher order functions earlier annomous classes was being used.

In Python, before the lambda expressions how was passing code to higher order functions been done ? Or this situation never happened and higher order functions and lambda expressions have existed since the same time.

 
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 just pass a function to a function...
 
Monica Shiralkar
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
In Java or Python the main purpose of lambda is to be able to pass anonymous functions to a higher order function.

And in Java, this previously required annomous classes. So the main advantage of using lambda over previous ways is that code is more readable and simpler.

In Python, as for purpose of lambda, it is same in Python too, to be able to pass anonymous functions to a higher order function. What about the main advantage as compared to the previous ways ?
 
What's that smell? I think this tiny ad may have stepped in something.
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic