This week's book giveaway is in the JDBC and Relational Databases forum.
We're giving away four copies of Murach's MySQL and have Joel Murach on-line!
See this thread for details.
Win a copy of Murach's MySQL this week in the JDBC and Relational Databases forum!
  • 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

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

 
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
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: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 Hand
Posts: 2925
13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 Hand
Posts: 2925
13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 Hand
Posts: 2925
13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 Hand
Posts: 2925
13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 Hand
Posts: 2925
13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 79019
375
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 Hand
Posts: 2925
13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 4757
71
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 Hand
Posts: 2925
13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 4757
71
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 Hand
Posts: 2925
13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 Hand
Posts: 2925
13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 15437
361
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 15437
361
  • Likes 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 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 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: 15437
361
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 8834
631
Mac OS X VI Editor BSD Java
  • 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: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 Hand
Posts: 2925
13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 Hand
Posts: 2925
13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Stephan van Hulst
Saloon Keeper
Posts: 15437
361
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No. Where did I say that a lambda expression is a reference to a function?

You can express a word using letters on paper, but you can also express a word using sounds from your larynx. "Word" is an abstract concept.

The same is true for a function. It's an abstract concept. You can express a function using a method definition, but you can also express it using a lambda expression.

A higher order function takes a reference to a function. A lambda expression returns a reference to the function that it expresses, but it is not a function itself.
 
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:No. Where did I say that a lambda expression is a reference to a function?

You can express a word using letters on paper, but you can also express a word using sounds from your larynx. "Word" is an abstract concept.

The same is true for a function. It's an abstract concept. You can express a function using a method definition, but you can also express it using a lambda expression.A higher order function takes a reference to a function.



Yes, my understanding upto here was the same


A lambda expression returns a reference to the function that it expresses


This is where my understanding has been wrong.

What does it mean  exactly when we say that "lambda expression returns a reference to the function that it expresses".   ?( I was thinking that it means that lambda expression internally automatically gets converted into the function which it expresses and  this is wrong)
 
Stephan van Hulst
Saloon Keeper
Posts: 15437
361
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, a lambda expression gets converted to a function and a reference to that function is returned. This does not mean that the lambda expression IS a function.

It's like the expression "2 + 3". It gets converted to the integer value 5 by the compiler, but the expression "2 + 3" is not the value 5 itself. It's just a piece of text in a source file.
 
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:Yes, a lambda expression gets converted to a function and a reference to that function is returned. This does not mean that the lambda expression IS a function.

It's like the expression "2 + 3". It gets converted to the integer value 5 by the compiler, but the expression "2 + 3" is not the value 5 itself. It's just a piece of text in a source file.




Yes. In that case why is it wrong when I say that "We can pass lambda expression to a higher order function and this lambda internally gets converted to a function".?  (I am not saying that "one can pass a function").
 
Junilu Lacar
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Stephan van Hulst wrote:Yes, a lambda expression gets converted to a function and a reference to that function is returned. This does not mean that the lambda expression IS a function.


Sorry to muddy the waters here but in the most general sense, a lambda is, in fact, a function. It's an anonymous function.

In Java, a lambda expression is converted at compile time to an anonymous implementation of a functional interface. It's kind of like compile-time water in that it takes the form of its container. In this sense, a Java lambda expression is technically not "unbound" to an object because it is an object behind the scenes.

In the above example, doBar(), doFoo(), getFoo(), and getBar() are higher-order functions (even though technically in Java, they are called methods).

Clear as mud yet?

You can see the above code run here: https://repl.it/@jlacar/LambdaFun
 
Junilu Lacar
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Additionally, the following will not compile if added to the previous example I gave:

It should be clear why the above statements will not compile even though both getFoo() and getBar() appear to return identical lambda expressions. So while the lambda literals in the example look identical, they are in fact unique and distinct from each other.
 
Junilu Lacar
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Monica Shiralkar wrote:Yes. In that case why is it wrong when I say that "We can pass lambda expression to a higher order function and this lambda internally gets converted to a function".?  (I am not saying that "one can pass a function").


Because you are again mixing contexts. A lambda is DOES NOT "internally get converted to a function." It is translated into an anonymous implementation of a Functional Interface and then instantiated. In Java terms, it is converted into an instance of an anonymous class.

When you say "a lambda expression is a function," you're using "function" in a general, non-Java sense. This is a correct statement.

When you say "a lambda expression gets converted to a function," you're talking about the Java implementation. Since Java doesn't technically have "functions" -- there are only methods of classes/objects, the statement is technically incorrect.
 
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

Junilu Lacar wrote:
In the above example, doBar(), doFoo(), getFoo(), and getBar() are higher-order functions (even though technically in Java, they are called methods).



Thanks.  I think this has simplified. In that case the below should be correct . Is it?

Lambda expression means an expression which can be referred by an object of the type functional interface.
And, a Java method which can take a Lambda expression as an argument will be instead called a Function and not method (more specifically, a higher order function).  (Also, a java method which returns a Function is also called a Higher order function ).

 
Junilu Lacar
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • 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:
In that case the below should be correct . Is it?

Lambda expression means an expression which can be referred by an object of the type functional interface.
And, a Java method which can take a Lambda expression as an argument will be instead called a Function and not method (more specifically, a higher order function).  (Also, a java method which returns a Function is also called a Higher order function ).


That is closer to showing a better understanding. However, you seem determined to use absolute terms even though the term "function" is used relative to its context. This is why you're unable to get on the same page as us. We know "function" is a relative term and that it's fungible with "method" in certain contexts whereas you seem to have a hard time pinning down in which contexts they are fungible/interchangeable or not. Plus, there are some grammatical issues that might have to do more with your command of English than your actual understanding.

For example, you wrote "can be referred by an object of the type functional interface" which is somewhat nonsensical and therefore not technically correct but I get a sense that the idea you wanted to express is close.

I would phrase that as "In Java, a lambda expression can be assigned to a reference variable with a functional interface type." That is what is happening in these lines of code from my example:

Here, foo and bar are reference variables whose declared types are functional interfaces FooFun and BarFun, respectively. The lambda expressions on the right hand side are being assigned to them. The result of these statements is that foo and bar will each reference an instance of an anonymous implementation of the FooFun and BarFun functional interfaces, respectively.

Also, this: "a Java method which can take a Lambda expression as an argument will be instead called a Function and not method (more specifically, a higher order function)" -- is more or less correct except that the words "will" and "not" which I highlighted imply absolute terms whereas to be true, that statement has to be taken relatively. Again, this is because you seem to want to apply a "rule" to any context when it's not appropriate to do so. It's all relative to context.

So a more accurate way to say that would be "a Java method which can take a Lambda expression as an argument can be referred to as a higher-order function when talking about functional programming in general."

A more complete statement would also be "A Java method that can take a lambda expression as an argument and/or returns a lambda expression can be referred to as a higher-order function when talking about functional programming in general." That same Java method, however, still is technically a method as far as Java is concerned.
 
Junilu Lacar
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Your original question was poorly phrased and that has led to confusion and frankly, somewhat ambiguous answers.

A better way to ask it would have been "Is a Java method also a function?" and the short answer would be "Yes, if you're talking about functional programming concepts in general." The long answer would be "Yes, if you're talking about functional programming concepts in general, a method can be considered as a function or even a high-order function. But it's still a 'method' as far as Java is concerned."

Do you see how it's important to make the distinction between the two contexts to clearly communicate your understanding?
 
Junilu Lacar
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Note that I included "and/or returns a lambda expression" in my full description above. An example of returning a lambda expression that makes a Java method also a higher-order function can be seen in this code:

In this example, the descriptorFor() method is also a higher-order function because of the lambda expression being returned on line 17. The actual return value is an object: an instance of an anonymous implementation of the Descriptor functional interface defined on line 22 and has been annotated accordingly to clearly express the intent.

I added this code to my LambdaFun example on repl.it and this is the output of that code:

Multiplying by 2
1 * 2 = 2
2 * 2 = 4
3 * 2 = 6
4 * 2 = 8
5 * 2 = 10
6 * 2 = 12
7 * 2 = 14
8 * 2 = 16
9 * 2 = 18
10 * 2 = 20
11 * 2 = 22
12 * 2 = 24

Multiplying by 3
1 * 3 = 3
2 * 3 = 6
3 * 3 = 9
4 * 3 = 12
5 * 3 = 15
6 * 3 = 18
7 * 3 = 21
8 * 3 = 24
9 * 3 = 27
10 * 3 = 30
11 * 3 = 33
12 * 3 = 36

Multiplying by 7
1 * 7 = 7
2 * 7 = 14
3 * 7 = 21
4 * 7 = 28
5 * 7 = 35
6 * 7 = 42
7 * 7 = 49
8 * 7 = 56
9 * 7 = 63
10 * 7 = 70
11 * 7 = 77
12 * 7 = 84

Multiplying by 12
1 * 12 = 12
2 * 12 = 24
3 * 12 = 36
4 * 12 = 48
5 * 12 = 60
6 * 12 = 72
7 * 12 = 84
8 * 12 = 96
9 * 12 = 108
10 * 12 = 120
11 * 12 = 132
12 * 12 = 144
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic