• 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
  • Tim Cooke
  • Ron McLeod
  • Devaka Cooray
  • Paul Clapham
Sheriffs:
  • paul wheaton
Saloon Keepers:
  • Tim Holloway
Bartenders:

Before Java8 did functional programming not exist in Java or was possible by passing anonymous class

 
Ranch Hand
Posts: 2983
13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Lambdas were introduced in Java 8. Before Java 8,  did functional programming not at all exist in Java or was still possible using anonymous classes ?  Was only lambdas introduced in Java8 or functional programming in Java itself was introduced in Java 8 only ?Thanks
 
Marshal
Posts: 28486
113
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:Was only lambdas introduced in Java8 or functional programming in Java itself was introduced in Java 8 only ?



Well no, the Stream interface was also introduced in Java 8, along with functionality which I think most people would classify as "functional programming". If you aren't one of those people then you'd have to accept that Java does not support functional programming at the current time, since no major changes have been made to Stream and its associated classes since Java 8.

Before Java 8,  did functional programming not at all exist in Java or was still possible using anonymous classes ?



I doubt whether you would find many people who would want to implement functional programming using only anonymous classes. I don't want to say it would have been impossible but I don't think it would have been worth anybody's time.
 
Marshal
Posts: 4910
624
VSCode Eclipse IDE Redhat MicroProfile Quarkus Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
While I've never used Scala myself, it is a functional programming language, which like Java, gets compiled to byte code that runs on the JVM.  

It is possible to write applications which use a combination of Scala and Java (you can call from one to the other), so I suppose that could have been an option to bring FP in to your Java apps.
 
Monica Shiralkar
Ranch Hand
Posts: 2983
13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Paul Clapham wrote:

Monica Shiralkar wrote:Was only lambdas introduced in Java8 or functional programming in Java itself was introduced in Java 8 only ?



Well no, the Stream interface was also introduced in Java 8, along with functionality which I think most people would classify as "functional programming". If you aren't one of those people then you'd have to accept that Java does not support functional programming at the current time, since no major changes have been made to Stream and its associated classes since Java 8.


Means, there was no gap between the time when functional programming was possible in Java and when Lambdas were available.

Does that mean whoever did passing annomous class to higher order functions instead of passing lambda expressions, did that only as one was not familiar with lambda ?
 
Paul Clapham
Marshal
Posts: 28486
113
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:Does that mean whoever did passing annomous class to higher order functions instead of passing lambda expressions, did that only as one was not familiar with lambda ?



I don't think so. Just because Java didn't support lambda expressions at some point in time, it doesn't follow that people who used Java at that point of time were unfamiliar with lambda expressions. I'm quite sure that many people who used Java at that time were totally familiar with functional programming including lambda expressions. They just couldn't use them in Java.
 
Monica Shiralkar
Ranch Hand
Posts: 2983
13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok. No I am talking about lambda expressions of Java only.
 
Paul Clapham
Marshal
Posts: 28486
113
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:Ok. No I am talking about lambda expressions of Java only.



I don't understand. Lambda expressions are a concept of mathematical logic. They are implemented in many computer languages including Java. So there's no such thing as "lambda expressions of Java only".
 
Bartender
Posts: 15743
368
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Didn't we already answer this question here?
https://coderanch.com/t/777533/languages/Lambda-fuctions-Python-compared-Java#3554430

You asked whether Java had higher order functions before Java 8. I said yes, Java's version of higher order functions already existed before Java 8. It might not have been possible to pass them lambda expressions or method references, but passing them a function object by way of an anonymous class instance was still possible.

Java was (and is) not really a functional programming language. But that doesn't mean that it doesn't support a form of functional programming.

Functional programming is rather a programming style than a technical feature. You can do it in pretty much any programming language. It's just that functional languages like Haskell, Erlang or JavaScript make it more easy and natural.
 
Marshal
Posts: 81605
593
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You are confusing anonymous classes with functions. It has always been possible to write pure functions in Java®, which would allow for programming in a functional style, as Stephan has already told you.
 
Monica Shiralkar
Ranch Hand
Posts: 2983
13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks all. Is it so that while Java was having higher order functions before Java 8 but largely a lot of higher order functions were added in Java 8.? Like the forEach method taking lambda expressions as argument.Is it so that in Java 8 many higher order functions were introduced (while earlier also few were there) and streams and lambdas was introduced. Another example is comparator taking lambda expression as argument. Comparator has been since earlier in Java, but was ability of comparator to take lambda expression as argument in Java from earlier or got added in Java 8?

And despite these added in Java 8, if someone wanted to use functional programming in Java but not comfortable yet with lambda expressions then they were using annomous classes only until they would get familiar with lambda expressions in Java.
 
Bartender
Posts: 7645
178
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ron McLeod wrote:While I've never used Scala myself, it is a functional programming language, which like Java, gets compiled to byte code that runs on the JVM.  

It is possible to write applications which use a combination of Scala and Java (you can call from one to the other), so I suppose that could have been an option to bring FP in to your Java apps.



Not to mention Scheme, which has been available via Kawa (and possibly other implementations) for a looong time, certainly years longer than Scala, even if it wasn't quite as hip.
 
Stephan van Hulst
Bartender
Posts: 15743
368
  • 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:Thanks all. Is it so that while Java was having higher order functions before Java 8 but largely a lot of higher order functions were added in Java 8.?


I wouldn't even call it "a lot".

In version 8, some higher order functions were added that in functional languages are considered "standard". In pretty much every functional language, you will find some form of the map, filter, and reduce functions. Without some form of these functions, your API can hardly be called "functional".

The Stream API isn't really the special part about Java 8 though. Technically, it would have been possible for any third party library to provide this API before Java 8. In fact, I seem to recall that RxJava was already widely in use well before Java 8 appeared, and many people used it in order to bring a more functional programming style to Java.

Java 8 is special because it allowed you to implement functional interfaces using lambda expressions and method references.

Like the forEach method taking lambda expressions as argument.


This is probably the worst example. While the forEach() method can be considered a higher order function, its existence is only justified because Java is NOT a functional language. A purely functional language has no need for a forEach function.

Another example is comparator taking lambda expression as argument. Comparator has been since earlier in Java, but was ability of comparator to take lambda expression as argument in Java from earlier or got added in Java 8?


Comparator does not accept lambda expressions. You mean, Comparator is a functional interface, and therefore can be implemented using a lambda expression.

No, you could not implement Comparator using a lambda expression before Java 8, because lambda expressions are a language feature that got introduced in Java 8.

However, even though you could not implement Comparator using a lambda expression before Java 8, methods that accepted a Comparator might still be considered a higher order function even before Java 8. The reason is simple: The Comparator interface represents a functional type. Objects that implement that interface can be considered functions, and methods that accept those objects can be considered higher order functions.

annomous


Anonymous.

And despite these added in Java 8, if someone wanted to use functional programming in Java but not comfortable yet with lambda expressions then they were using annomous classes only until they would get familiar with lambda expressions in Java.


I don't think there would have been many of those people. Most people that were already familiar with functional programming would also already have been familiar with lambda expressions. Paul Clapham already pointed this out to you.
 
Monica Shiralkar
Ranch Hand
Posts: 2983
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:.


This is probably the worst example.
ok. What would be a good simple example?

A
Comparator does not accept lambda expressions. You mean, Comparator is a functional interface, and therefore can be implemented using a lambda expression.



Sorry. I mistakenly said comparator instead of Collections.sort. like the below:



And wanted to ask that while lambdas were introduced in Java 8, was it possible to write to above code using anonymous class before Java 8 ?



I don't think there would have been many of those people. Most people that were already familiar with functional programming would also already have been familiar with lambda expressions. Paul Clapham already pointed this out to you.



I have seen so much code written using anonymous classes instead of lambda. May be in java 8, people had learning curve to learn few things and many people learnt passing code to functions but were still to learn lambdas so used anonymous upto then.
 
Paul Clapham
Marshal
Posts: 28486
113
Eclipse IDE Firefox Browser MySQL Database
  • 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:I have seen so much code written using anonymous classes instead of lambda. May be in java 8, people had learning curve to learn few things and many people learnt passing code to functions but were still to learn lambdas so used anonymous upto then.



Sure. Even today, 15 years after Java 8 was released, you'll find Swing code using anonymous classes to respond to events. It's not necessarily that there's a learning curve, it's just easier to write code the same way as you did before, or the same way as the tutorials you're copying from. And most people wouldn't go back and change existing working code to use lambdas, either.
 
Stephan van Hulst
Bartender
Posts: 15743
368
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Monica Shiralkar wrote:What would be a good simple example?


I already mentioned the quintessential functions map, filter and reduce.

And wanted to ask that while lambdas were introduced in Java 8, was it possible to write to above code using anonymous class before Java 8 ?


Yes.

Paul Clapham wrote:Sure. Even today, 15 years after Java 8 was released, you'll find Swing code using anonymous classes to respond to events. It's not necessarily that there's a learning curve, it's just easier to write code the same way as you did before, or the same way as the tutorials you're copying from. And most people wouldn't go back and change existing working code to use lambdas, either.


Let's not forget that many event listeners still can't be implemented using lambda expressions, because they are not functional interfaces: Either they contain multiple abstract methods (example: MouseListener) or they are an abstract class and not an interface (example: AbstractAction).

I just did a quick count of the interfaces of Java 8 that extend java.util.EventListener, and not even 50% of them are functional interfaces. That's not even taking into account all the abstract classes that people might want to extend instead.
 
Monica Shiralkar
Ranch Hand
Posts: 2983
13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok. Understood. Thanks. So Collections.sort is on of the higher order functions which were already there in Java before Java 8, and in Java 8 the capability to pass lambda to it instead of annomous function got added
 
Stephan van Hulst
Bartender
Posts: 15743
368
  • 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.
 
Campbell Ritchie
Marshal
Posts: 81605
593
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Monica Shiralkar wrote:. . . . So Collections.sort is on of the higher order functions . . . .

No. I don't think it is a higher order function. In fact, I think Collections.sort() isn't a function at all because it doesn't return a result.

. . . capability to pass lambda to it instead of annomous function . . .

I thought we had established there is no such things as an anonymous function. Maybe a reference to an instance of an anonymous class as a Comparator.
 
Monica Shiralkar
Ranch Hand
Posts: 2983
13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
But a method which can take a function as parameter is a higher order function. So that way Collections.sort should be a higher order function as we can pass lambda to it.
 
Stephan van Hulst
Bartender
Posts: 15743
368
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:No. I don't think it is a higher order function. In fact, I think Collections.sort() isn't a function at all because it doesn't return a result.


Just for context, it was I that said that Collections.sort() could be considered a higher order function in my answer here.

Within the context of functional programming, I use this term to refer to an operation that accepts a function as a parameter. To me, a comparator instance is a good example of a function object, and therefore the sort() method can informally be referred to as a "higher order function", even if it's not correct in the strictest sense (mostly because Java doesn't really have functions as first class members in the first place).

The vocabulary I've used in this topic (and Monica's other topic I've linked to) is used primarily to refer to abstract notions from Functional Programming in general, not to how things are named in Java specifically.

Monica Shiralkar wrote:But a method which can take a function as parameter is a higher order function. So that way Collections.sort should be a higher order function as we can pass lambda to it.


Be careful. You need to be aware of the context when you say or read things like this. I said that Collections.sort() can be considered a higher order function. I didn't say that it IS a higher order function.

So, if the context is something like "Does the Java Language Specification say anything about Collections.sort() being a higher order function?" then the answer is no, sort() is not a higher order function because Java doesn't have functions.

If the context is something like "In an informal conversation about Functional Programming in Java, can Collections.sort() be considered a higher order function?" then the answer is yes, because every functional programmer will understand that you can pass a function-like object to it.
 
Monica Shiralkar
Ranch Hand
Posts: 2983
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:


So, if the context is something like "Does the Java Language Specification say anything about Collections.sort() being a higher order function?" then the answer is no, sort() is not a higher order function because Java doesn't have functions.
.



Does it mean Java does not have higher order functions?

So, if the conversation is informal, we can say that in Java lambda expressions are used to be able to being passed to higher order functions. What can we say for this if the conversation is not informal ?
 
Stephan van Hulst
Bartender
Posts: 15743
368
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Things are not so black and white. It depends on context: Who is your audience? What information are you trying to get across? How much does it matter to be precise in your wording?

I feel that you're spending way too much time on theorizing, and not enough time actually applying the abstract concepts you're learning about.

If I didn't think you're already spending your time and attention on too many different things at the same time, I'd tell you to go and actually write a program in a purely functional language like Miranda, Haskell or Erlang.
 
Monica Shiralkar
Ranch Hand
Posts: 2983
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'd tell you to go and actually write a program in a purely functional language like Miranda, Haskell or Erlang.



But I was talking only about Java functional programming, and it should be fine if someone wants to not get into those other languages.

I got the answer which I was looking for that before Java 8, for things like comparator.sort, one had to pass anonymous class instance instead of simply passing a lambda. Thanks all.The only thing that had confused me was when it was said that Java does not have higher order functions, which I believe it does. Anyway, will ignore that part and rest I learnt from this post that before Java 8, for things like comparator.sort, earlier anonymous instance  was required to be passed and after Java 8 it is simply the lambda.
 
Stephan van Hulst
Bartender
Posts: 15743
368
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Monica Shiralkar wrote:before Java 8, for things like comparator.sort, earlier anonymous instance  was required to be passed and after Java 8 it is simply the lambda.


Not required. It was just one way you could do it.

Another way was to simply write a regular named class and create an instance of it. And that is also still an option this day.
 
Monica Shiralkar
Ranch Hand
Posts: 2983
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:before Java 8, for things like comparator.sort, earlier anonymous instance  was required to be passed and after Java 8 it is simply the lambda.


Not required. It was just one way you could do it.

Another way was to simply write a regular named class and create an instance of it. And that is also still an option this day.



Ok. So best way ofcourse is to simply pass a lambda expression. And before lambda came in Java 8, a longer way was using anonymous class. And an even longer way can be like a regular named class like you said.
 
Stephan van Hulst
Bartender
Posts: 15743
368
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Monica Shiralkar wrote:So best way ofcourse is to simply pass a lambda expression.


Again, no. The best way depends on what is the easiest to read and understand. As I mentioned earlier, I find a method reference to be clearer in 95% of all cases. I rarely use lambda expressions at all.

And of course, if you already have a class somewhere that performs the exact functionality you are looking for, then there also is no reason to use a lambda expression.
 
Monica Shiralkar
Ranch Hand
Posts: 2983
13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ok. I will check on method reference.
 
You got style baby! More than this tiny ad:
Paul Wheaton's 16th Kickstarter: Gardening playing cards for gardeners and homesteaders
https://coderanch.com/t/889615/Paul-Wheaton-Kickstarter-Gardening-playing
reply
    Bookmark Topic Watch Topic
  • New Topic