• 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
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
  • Bear Bibeault
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Piet Souris
  • salvin francis
  • Stephan van Hulst
Bartenders:
  • Frits Walraven
  • Carey Brown
  • Jj Roberts

Is higher order function actually a method and not a function?

 
Ranch Foreman
Posts: 2067
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is said that higher order function is a function which takes a function as argument. In my below code which is a simple example of higher order function, higher order function is myMethod which is infact a method and not a function. Is higher order function actually a method and not a function and thus the name is somewhat misleading?

Thanks

 
Sheriff
Posts: 15995
265
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are mixing contexts -- the "function" in "higher order function" is in the mathematical sense. The "method" you're talking about is in object-oriented programming terms. To say that using "function" to refer to a "method" is misleading is kind of like saying that calling the round thing that allows you to control the direction of a car a steering "wheel" is misleading because we also call the four round things on the car that touch the ground "wheels."
 
Junilu Lacar
Sheriff
Posts: 15995
265
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also, a higher order function is a function (or in object-oriented terms, a method) that does at least one of:
a) takes another function/method as an argument
b) returns another function/method as a result
 
Junilu Lacar
Sheriff
Posts: 15995
265
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you're really having trouble making the distinction between function vs. method, then just think of "function" as something that you can call to do something for you, including give back the result of whatever that something is. In other words, it's just another logical chunk of work/code that you can treat as a separate entity.
 
Monica Shiralkar
Ranch Foreman
Posts: 2067
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Junilu Lacar wrote:If you're really having trouble making the distinction between function vs. method, then just think of "function" as something that you can call to do something for you, including give back the result of whatever that something is. In other words, it's just another logical chunk of work/code that you can treat as a separate entity.



Thanks.I call something as method if it is defined in a class and would belong to the object or the class (in case of static). I call something as method if it does not belong to object or class and we use it for one time (not again and again by giving it name).

In my example, I would call as the function and myMethod as the method.

But I think we need to call myMethod as "Function" instead of "Method" and then not go with the literal meaning.
 
Monica Shiralkar
Ranch Foreman
Posts: 2067
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Junilu Lacar wrote: the "function" in "higher order function" is in the mathematical sense.



I am trying to understand this. In any sense how is it "function" when Function is instead something like
 
Junilu Lacar
Sheriff
Posts: 15995
265
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just think of "function" as any code that takes some input and does something to it then possibly returns a result. If you use this definition of function, then the x that comes before the -> is the input (the lambda parameter) and the x*2 is the "something" that's done with the input. The product of x and 2 is the result returned by that lambda expression. Therefore, it's a function.
 
Monica Shiralkar
Ranch Foreman
Posts: 2067
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I understand that in my code above is clearly a function.  What I am trying to understand that how is of my code above a function in any sense.
 
Junilu Lacar
Sheriff
Posts: 15995
265
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Don't overthink this. It's not complicated if you don't let yourself get hung up by the different contexts and words used. Just see "function" as another way of saying "method" or "lambda" in Java. In Java parlance, we don't formally name anything a function per se but if you just treat "function" as a synonym we've adopted from another domain (mathematics) used to refer to what we call methods and lambdas, then I don't really see why this should be confusing or misleading.
 
Monica Shiralkar
Ranch Foreman
Posts: 2067
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Method - a code that does something and we give it a name and then can use it again and again. It belongs to object or class (if static).
Function - a code that does something and we do not give it a name and just use it once. It does not belong to object or class.

Going by the above is clearly a function and myMethod is a method.
 
Junilu Lacar
Sheriff
Posts: 15995
265
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Again, you're mixing contexts. Stop doing that. You're just confusing yourself when there's really no need to be confused. Your definition of function is not correct. Math.sqrt() is a method, right? Well, guess what: it's also a function.
 
Monica Shiralkar
Ranch Foreman
Posts: 2067
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Junilu Lacar wrote:Again, you're mixing contexts. Stop doing that. You're just confusing yourself when there's really no need to be confused.



Thanks, sure. Now I have started calling Map, Reduce, Filter and other higher order functions as Functions (instead of Methods which I was earlier calling).

Your definition of function is not correct. Math.sqrt() is a method, right? Well, guess what: it's also a function.



Sure. Is there any reason for calling it so, which I can try and understand?
 
Marshal
Posts: 71070
292
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is convention that Java® methods are called methods; in C they are called functions, even if they don't represent a mathematical function.
 
Junilu Lacar
Sheriff
Posts: 15995
265
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Monica Shiralkar wrote:Thanks, sure. Now I have started calling Map, Reduce, Filter and other higher order functions as Functions (instead of Methods which I was earlier calling).


You can call it whatever you feel like calling it.

Personally, I would still refer to map(), reduce(), and filter() as "methods" if I were referring to them in a purely/mostly Java language sense. If I was talking about map, reduce, and filter in a more general mathematics or functional programming sense, I might prefer to call them "functions" instead. To me, it all depends on the context.
 
Monica Shiralkar
Ranch Foreman
Posts: 2067
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Junilu Lacar wrote: If I was talking about map, reduce, and filter in a more general mathematics or functional programming sense, I might prefer to call them "functions" instead..



In the same context which you mentioned , that is Functional programming I was thinking Function is this kind of a thing but now I know that in   , not only is a function but map is also a function.  
So, both are functions.
 
Junilu Lacar
Sheriff
Posts: 15995
265
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
From what you're showing I would just refer to x -> x * 2 as a lambda expression because that's the official term we use in Java. And map() is still a method. You didn't show anything there that would lead me to think that the context was more about functional programming in general than functional-style programming in Java. For the latter, I would still prefer to use the Java terms.
 
Master Rancher
Posts: 3721
47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
An additional consideration: in Java, we also have the java.util.function.Function interface, which is distinct from other java.util.function types such as BiFunction, Consumer, IntFunction, etc - which can also be represented by lambdas.  I would avoid saying "function" in Java unless you mean a java.util.function.Function, to avoid confusion.

To be fair, that situation actually does apply in several places in this thread... e.g. in

The lambda used for the argument to map() is, indeed, a Function.

However, when you talk about a "higher order function", that's functional programming terminology, not Java.  Which is what led to confusion in this thread.
 
Monica Shiralkar
Ranch Foreman
Posts: 2067
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In Functional programming (introduced in Java 8 onwards), we now can pass functions (which do not belong to object or class and are to be used only once so we dont give it any name), to higher order functions.

So is not involving functional programming and is not function which does not belong to any class or object and we are passing it to higher order function?
 
Junilu Lacar
Sheriff
Posts: 15995
265
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Monica Shiralkar wrote:... is not function which does not belong to any class or object and we are passing it to higher order function?


You misunderstand how lambda expressions work in Java. They actually do belong to an object: the lambda gets translated into the implementation of a functional interface so technically it does belong to an object. In the case of map() you can see what functional interface the lambda will implement by reading the JavaDocs: https://docs.oracle.com/javase/8/docs/api/java/util/stream/Stream.html#map-java.util.function.Function-

In your example, the lambda becomes the implementation of the Function.apply() method.

I still don't understand your insistence in using "function" instead of "lambda expression" when you're talking mainly in terms of Java concepts. I think your use of "function" is more confusing or at least has more cognitive weight because I have to translate it in my head to "method" or "lambda".
 
Mike Simmons
Master Rancher
Posts: 3721
47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Monica Shiralkar wrote:So is not involving functional programming


It does involve functional programming,  but you're doing it in Java, and you're here talking with a group people whose background is more "Java programming" than "functional programming".  So when terminology is different between the two different contexts, it's confusing, and to help with that, we here try to be consistent with Java terminology rather than functional terminology.  Neither is wrong or right in general, but it's a good idea to try to remember what group you're talking with and be somewhat consistent in terminology.
 
Monica Shiralkar
Ranch Foreman
Posts: 2067
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Junilu Lacar wrote:so technically it does belong to an object.


Thanks. Yes technically it does because its declared in functional interface.
 
Monica Shiralkar
Ranch Foreman
Posts: 2067
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Mike Simmons wrote:it's a good idea to try to remember what group you're talking with and be somewhat consistent in terminology.



Thanks. Say, I am talking to a group of people about Java and more specifically we are talking about one of the new features introduced in Java 8 that is lambda expressions.  In this case shall I be calling map as method or function and shall I be calling x->x*2 as method or function. My thinking based on what I was originally thinking and based on what I understood in this thread is that we can call both as Functions. Is that correct now? We can call that map is a function , a higher order function which can take a lambda function as argument.
 
Saloon Keeper
Posts: 12493
269
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It really isn't that difficult.

When the general topic of the discussion is functional programming, use the term "function". When the general topic is Java programming, use "method".
 
Stephan van Hulst
Saloon Keeper
Posts: 12493
269
  • Likes 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Monica Shiralkar wrote:lambda function


There is no such thing. It's like saying "digit number".

Higher order functions can take other functions as their arguments. You can use a "lambda expression" to express a function, the same way you can use digits to express a number. It's important to realize that a function that takes a number does not take digits, and a function that takes another function does not take a "lambda".
 
Monica Shiralkar
Ranch Foreman
Posts: 2067
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Stephan van Hulst wrote: a function that takes another function does not take a "lambda".


But isn't map(a function that takes another function), not taking a lambda in
 
Stephan van Hulst
Saloon Keeper
Posts: 12493
269
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You completely missed what I was trying to tell you.

A lambda expression is not a function. It is a jumble of letters on paper (or rather a source file) that expresses a function. The compiler translates a lambda expression to a bytecode representation of the function, and the JVM reads the bytecode and translates it to an in-memory representation of the function.

"Function" is an abstract concept. It exists in our minds and to convey it to other people or to a computer, we need some sort of language to write it down. A lambda expression is just a way of writing down a description of the function. I could write the same function like this:

This expresses the exact same function that you expressed with your lambda expression.

A higher order function doesn't take a "lambda". It takes a reference to a function. Whether that function was originally expressed using a lambda expression or a method or indeed a prose description like the one I wrote above is inconsequential.
 
Marshal
Posts: 7866
547
Mac OS X VI Editor BSD Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Monica Shiralkar wrote:But isn't map(a function that takes another function), not taking a lambda in


@OP Look what Racket language documentation says here.

A lambda expression creates a function.



And now look what Stephan wrote earlier:

Stephan van Hulst wrote: a function that takes another function does not take a "lambda".



That's exactly, right? Lambda expression creates a function which is passed on as an argument. And that is all here.
 
Monica Shiralkar
Ranch Foreman
Posts: 2067
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks

Stephan van Hulst wrote:

A higher order function doesn't take a "lambda". It takes a reference to a function. Whether that function was originally expressed using a lambda expression or a method or indeed a prose description like the one I wrote above is inconsequential.




So higher order function takes reference of another function (in the form of lambda). So, it means lambda is reference to a function in the sense that when we give a lambda like this  , it is internally creating a function for this automatically. Understood this part.  

So am I wrong when I say that a method is one to which we give a name and can reuse it again and again whereas function is what we use only once and we dont give it any name as it is to be used just once and it gets created automatically using the lambda?
 
Monica Shiralkar
Ranch Foreman
Posts: 2067
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Liutauras Vilda wrote:

That's exactly, right? Lambda expression creates a function which is passed on as an argument. And that is all here.



So what I understand is lambda is reference to a function. So we pass reference to function to a higher order function and it automatically creates a function using it.
 
I'm not dead! I feel happy! I'd like to go for a walk! I'll even read a tiny ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic