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

Why are methods like forEach called higher order function instead of higher order methods?

 
Ranch Hand
Posts: 2925
13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
forEach is a method. Why is it called higher order function instead of calling it higher order method ? Thanks
 
Saloon Keeper
Posts: 15448
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"Higher order function" is an established term in functional programming. When Java started adding some concepts from functional programming, the community also started using vocabulary from functional programming.

Referring to it as a "higher order method" serves no real purpose, and could even be considered wrong, because it implies that it accepts other methods. It doesn't. It accepts functions.
 
Monica Shiralkar
Ranch Hand
Posts: 2925
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:

Referring to it as a "higher order method" serves no real purpose, and could even be considered wrong, because it implies that it accepts other methods. It doesn't. It accepts functions.



But it is a method that accepts function.
 
Stephan van Hulst
Saloon Keeper
Posts: 15448
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Right, and there is no established term specifically for a method that accepts a function. So instead, we use a term borrowed from FP, because anyone who is familiar with FP will instantly understand what is meant.
 
Monica Shiralkar
Ranch Hand
Posts: 2925
13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Understood.thanks.
 
Monica Shiralkar
Ranch Hand
Posts: 2925
13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When we have an annomous class implementing a functional interface, and having implementation for the method of this functional interface, then pass object of this annomous class to the higher order function, then what we are actually passing is an object. So why is it said that we are passing function to the higher order function?
 
Stephan van Hulst
Saloon Keeper
Posts: 15448
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Monica Shiralkar wrote:annomous


Anonymous. From "an-", Greek prefix meaning "without"; "onym", Greek word meaning "name", and "-ous", an English suffix making it an adjective.

Monica Shiralkar wrote:When we have an annomous class implementing a functional interface, and having implementation for the method of this functional interface, then pass object of this annomous class to the higher order function, then what we are actually passing is an object. So why is it said that we are passing function to the higher order function?


Because in functional languages, that's what a function is. The word "function" in functional languages almost has the same meaning that the word "object" has in object oriented languages. That's why in Java, a method is NOT a function. You can't pass a method around as if it were an object.
 
Monica Shiralkar
Ranch Hand
Posts: 2925
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:

Monica Shiralkar wrote:annomous


The word "function" in functional languages almost has the same meaning that the word "object" has in object oriented languages..



So when using Anonymous class way (before Java 8), instead of saying that to the higher order function we are passing object of an annomous class implementing a functional interface, and having implementation for the method of this functional interface,
we can simply say we are passing an  anonymous function to it ?
 
Stephan van Hulst
Saloon Keeper
Posts: 15448
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Monica Shiralkar wrote:annomous


Anonymous.

Monica Shiralkar wrote:instead of saying that to the higher order function we are passing object of an annomous class implementing a functional interface, and having implementation for the method of this functional interface,
we can simply say we are passing an  anonymous function to it ?


You could, but since Java is still mostly used by object-oriented programmers, it'll probably be clearer if you stick with "anonymous class instance".
 
Monica Shiralkar
Ranch Hand
Posts: 2925
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: "anonymous class instance".



Ok. But from just anonymous class instance, it is not clear that which annomous class instance. In actual it is instance of annomous class which implements the functional interface.
 
Saloon Keeper
Posts: 27708
196
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
Bear in mind also that mathematical terms and computer science terms don't always get used with precisely the same meaning and especially that when describing computer science in the abstract, the mathematical terms and meaning are more likely to be used as opposed to language-specific terms and meanings.

A "function" should be contrasted with "funtionality", meaning serving a purpose, as opposed to a specific implementation or, say, a named lambda.
 
Stephan van Hulst
Saloon Keeper
Posts: 15448
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Monica Shiralkar wrote:annomous


Anonymous.

Monica Shiralkar wrote:But from just anonymous class instance, it is not clear that which annomous class instance.


I'm not sure what you mean. What isn't clear? And why is that important?
 
Monica Shiralkar
Ranch Hand
Posts: 2925
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:
I'm not sure what you mean. What isn't clear? And why is that important?



What is not clear by anonymous class instance is that instance of which anonymous class. By instance of annomous class it may mean any annomous class it is not instance of any annomous class that we pass. So, I think It should be specified that we pass instance of annomous class which implements the functional interface.
 
Stephan van Hulst
Saloon Keeper
Posts: 15448
363
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Monica Shiralkar wrote:annomous


Anonymous.

Monica Shiralkar wrote:annomous


Anonymous.

Monica Shiralkar wrote:annomous


Anonymous.

Monica Shiralkar wrote:annomous


Anonymous.

Monica Shiralkar wrote:So, I think It should be specified that we pass instance of annomous class which implements the functional interface.


Why? Why is that important? What does it matter?
 
Bartender
Posts: 2412
13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It does not matter if a class is anonymous or not. During runtime, the Java runtime environment will create an instance for that class.
 
Marshal
Posts: 28138
95
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

Monica Shiralkar wrote:So, I think It should be specified that we pass instance of annomous class which implements the functional interface.



I don't see why that is true. As far as I can see it's possible to write a class (with a name, of course) which implements the functional interface, and then use an instance of that (non-anonymous) class.
 
Monica Shiralkar
Ranch Hand
Posts: 2925
13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Himai Minh wrote:It does not matter if a class is anonymous or not. During runtime, the Java runtime environment will create an instance for that class.



But you cannot pass that to higher order functions. You can either pass lambda expressions (Java 8 onwards) or an instance of an annonymous class that implements a functional interface.
 
Monica Shiralkar
Ranch Hand
Posts: 2925
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? Why is that important? What does it matter?



I think it because when we say "anonymous class instance", it's a general thing. Whereas we can't pass a general thing to the higher order function, we have to pass a specific anonymous class instance, that is the one of class that implements functional interface, not any anonymous class instance.
 
Stephan van Hulst
Saloon Keeper
Posts: 15448
363
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The same is true for ANY method that has an interface X as a parameter. When you pass an anonymous class instance, the class must implement the interface X, regardless of whether the interface is a functional interface or not.

Now, you could insist on saying "anonymous class that implements a functional interface" whenever you're using a method that accepts a functional interface, but it really doesn't matter.

You also could insist on saying "higher order function" whenever you're using a method that accepts a functional interface, but again, most people really don't care.

You're assigning way too much importance to all of this. You understand how the code works, what you can do with it, and you understand what people mean when they say "anonymous class", "function", "functional interface" and "higher order function", or you'll be able to determine it from context. Stop worrying about it.
 
Monica Shiralkar
Ranch Hand
Posts: 2925
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: a method that accepts a functional interface



I think in actual it is an object that it accepts (not interface).

Stephan van Hulst wrote:
You're assigning way too much importance to all of this. You understand how the code works, what you can do with it, and you understand what people mean when they say "anonymous class", "function", "functional interface" and "higher order function", or you'll be able to determine it from context. Stop worrying about it.



Sure. Thanks
 
Marshal
Posts: 79060
375
  • 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 in actual it is an object that it accepts (not interface). . . .

I am getting the impresssion that we are going in circles about things that don't actually matter.
A parameter has a type, which is what Stephan means, and at runtime a copy of a reference to an object of that type, not the actual object, is passed.
 
Monica Shiralkar
Ranch Hand
Posts: 2925
13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:at runtime a copy of a reference to an object of that type, not the actual object, is passed.



Yes, but that is not interface.
And we can ignore it as it is not important like you had earlier said.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic