Before Java8 did functional programming not exist in Java or was possible by passing anonymous class
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
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.
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
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.
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
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 ?
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
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.
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
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".
-
2 -
-
Number of slices to send:Optional 'thank-you' note:
-
-
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.
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
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.
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
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.
-
1 -
-
Number of slices to send:Optional 'thank-you' note:
-
-
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.
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
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.
-
1 -
-
Number of slices to send:Optional 'thank-you' note:
-
-
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.
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
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.
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
Monica Shiralkar wrote:annomous
Anonymous.
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
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.Monica Shiralkar wrote:. . . . So Collections.sort is on of the higher order functions . . . .
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.. . . capability to pass lambda to it instead of annomous function . . .
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
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.
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
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 ?
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
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.
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
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.
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
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.
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
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.
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
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.
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
| 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 |










