• 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
  • Bear Bibeault
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Junilu Lacar
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Jj Roberts
  • Tim Holloway
  • Piet Souris
Bartenders:
  • Himai Minh
  • Carey Brown
  • salvin francis

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

 
Ranch Foreman
Posts: 1988
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: 15983
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: 15983
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: 15983
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: 1988
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: 1988
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: 15983
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: 1988
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: 15983
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: 1988
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: 15983
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.
 
Do Re Mi Fa So La Tiny Ad
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic