• 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 the functional programming syntax of Java less self explanatory then the old syntax?

 
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
Is the functional programming syntax of Java less self explanatory then the old syntax?
For example below is a code in old syntax (Java 7) for a sample (Apache Spark word count):






And below is its equivalent code in functional programming syntax:





Isnt the first code more self explanatory. If so then what is the way to adatpt to the functional programming syntax if you are used to more self explanatory old syntax?

thanks
 
Ranch Hand
Posts: 127
2
Monad Java Linux
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
One of the features of functional programming is to highlight the operations without detail clutter.
 
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Using old syntax, the code becomes self explanatory if you go through the lines of code.
Using functional programming, the code becomes self explanatory if you already have the knowledge of functional programming. So one needs much higher level of knowledge to understand code with functional programming.
 
Gerard Gauthier
Ranch Hand
Posts: 127
2
Monad Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I forgot to mention that the functional programming features of Java provide sane and safe high level features to the language.
 
Saloon Keeper
Posts: 15484
363
  • 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:Isnt the first code more self explanatory.


What's more self explanatory about it? It doesn't explain anything more than the second version does. It's just more verbose.

SwapanB Biswas wrote:So one needs much higher level of knowledge to understand code with functional programming.


This is not a good argument. You need knowledge about anonymous classes to understand the first version. You need knowledge about lambda expressions to understand the second version. There's not anything inherently more difficult about lambda expressions.
 
Monica Shiralkar
Ranch Hand
Posts: 2925
13
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

What's more self explanatory about it? It doesn't explain anything more than the second version does. It's just more verbose.



Understanding 3 lines step by step is easier than understanding 1 big line.
 
Marshal
Posts: 4491
572
VSCode Eclipse IDE TypeScript Redhat MicroProfile Quarkus Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Do you really prefer something like to ??
 
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
The reason I find the below code easy to understand is that I see we are creating object of a class which implements the Function2 interface and overrides its call method.



With the below code , I do not easily infer the above visible details.

(x, y) -> x + y

thanks
 
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why do you need to know anything about what sort of object you are creating?
 
Rancher
Posts: 4801
50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I suspect that's down to you being more used to the former syntax than the latter.
 
Bartender
Posts: 667
14
TypeScript Fedora
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you dont know the functions being done then it can be confusing.  But if you look at the docs you can figure it out.   I wasnt sure at first what it did but after looking at the docs i understand now.

I think the variable names are what were most important here.  The functional version looks way better.
 
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

Why do you need to know anything about what sort of object you are creating?



I need to learn that the way to implement this is to implement the call method of Function2 interface.I need to know that this is the way to do it.




Using this I understand that there is 'some' Interface which I need to implement whose 'some' method I need to override.


Isnt it required to learn that which Interface we are supposed to implement?
 
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


This line tells me that I need to implement 'some' interface which has 1 method which I will override such that it takes a line param and returns the spitted line.Then I need to create object of this class which implements this 'some' interface and pass it as an argument to flatMap method. I know that it is 'some' interface but I dont know which interface was I supposed to implement.
 
Gerard Gauthier
Ranch Hand
Posts: 127
2
Monad Java 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:

Isnt it required to learn that which Interface we are supposed to implement?



I think, in this case, its better to think of function signatures than to think of which interfaces to implement. Remember we are in the realm lambdas and functional programming here.
 
Campbell Ritchie
Marshal
Posts: 79151
377
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
prefer to think what I am putting in and what I am getting out. If I get it wrong, it won't compile.
 
Stephan van Hulst
Saloon Keeper
Posts: 15484
363
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can easily find the interface that the lambda expression implements by looking at the method documentation of flatMap(). This is no different than in the first version of the code, where you only know that you have to create an instance of FlapMapFunction when you look at the documentation.

The entire point of lambda expressions is that once it's written, it no longer matters what the name of the functional interface is that they implement. You can just focus on what it does, rather than the plumbing that goes on underneath.
 
Gerard Gauthier
Ranch Hand
Posts: 127
2
Monad Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I find people have a hard time conceptualizing functional programming and why you have to do things certain ways... I had that problem too but it went away when I started thinking about data/objects in a functional sense. Here're some things to consider when you are approaching Java objects as a functional object.

1. The object is impenetrable and immutable. You can't touch the data contained in the object, and you have no idea what it is.
2. You can communicate with the object by passing it functions of a specific signature. These functions can use the object's value to produce new values but never change the object's value.
3. You can never and I mean never modify the object. When you need to produce new things from a functional thing, you always produce a new thing by using the value to produce a new object.

Do you see why functional things are safe for multi-thread apps? Once you start treating a Java object as functional, it becomes an impenetrable and immutable object which is safe to pass around because it never changes.
 
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 think, in this case, its better to think of function signatures than to think of which interfaces to implement. Remember we are in the realm lambdas and functional programming here.





Ok, in that case what all is the exact understanding from this line ?

 
Gerard Gauthier
Ranch Hand
Posts: 127
2
Monad Java Linux
  • 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 this case, its better to think of function signatures than to think of which interfaces to implement. Remember we are in the realm lambdas and functional programming here.





Ok, in that case what all is the exact understanding from this line ?



Are you asking me? I only have 2 weeks experience with Java but I'll give it a shot. Traditionally the function passed to a flatmap has a signature:

Where 'a is the data type and T is the container/collection, so if the example uses a traditional flatmap function signature then you are taking a container of lines('a) and producing a new container of words('b).
 
Gerard Gauthier
Ranch Hand
Posts: 127
2
Monad Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please note: In the example I provided, 'a and 'b can be the same type. The flatmap signature just states that they don't have to be the same type.
 
Gerard Gauthier
Ranch Hand
Posts: 127
2
Monad Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you really want to understand flatmap and map then you should consider a simple Java type like Optional. The Optional type will expose you to map and flatmap  but in a simple container.

Optional
 
Stephan van Hulst
Saloon Keeper
Posts: 15484
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:Ok, in that case what all is the exact understanding from this line ?


map() transforms each element to exactly one different element. flatMap() transforms each element to any number of different elements.

So a map() operation could transform [A, B, C] to [95, 85, 75], (exactly one output for each input) while a flatMap() operation can transform [A, B, C] to ["Bob", "Carrol", "Alice", "Eve", "Dave"] (one, more than one, or less than one output for each input).

With that in mind, passing line -> Arrays.asList(line.split(" ")) to the flatMap() function, results in each input line being split into zero, one or more than one output words:
becomes
 
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.

I understood what it does but why is it said that we are passing 'function' to the method as parameter while we are instead passing 'object' of the class which implements the Interface whose method is being overridden.

JavaRDD words =
   lines.flatMap(line -> Arrays.asList(line.split(" ")));
 
Stephan van Hulst
Saloon Keeper
Posts: 15484
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That's just the terminology.

You can think of a function in Java as any object that implements a functional interface. When instead of using a lambda expression you create an instance of an anonymous type, you can refer to that instance as a function as well.

It's helpful to think of functions (note that I didn't say methods) as objects. In fact, in many functional languages, functions ARE objects and all objects are functions.
 
Gerard Gauthier
Ranch Hand
Posts: 127
2
Monad Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Stephan van Hulst wrote:That's just the terminology.
In fact, in many functional languages, functions ARE objects and all objects are functions.



That's one of the great features of high level functions. You can pass functions around like data(or objects).
 
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

It's helpful to think of functions (note that I didn't say methods) as objects.



Does it mean that a java method is known as functions only if it is method of  some functional interface. In all other cases it is called as methods.

You can think of a function in Java as any object that implements a functional interface(or objects).





Object that implements a functional interface or Object of a class that implements a functional interface?


prefer to think what I am putting in and what I am getting out. If I get it wrong, it won't compile.



So this is regarding how to use it. Another thing is where can it be used. This can be used only in case of functional interface but while trying to use it how to relate it to the functional interface.


 
Stephan van Hulst
Saloon Keeper
Posts: 15484
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Monica Shiralkar wrote:Does it mean that a java method is known as functions only if it is method of  some functional interface.


The object that implements the functional interface is referred to as a function. However, the lines get a bit blurry here because sometimes methods that return something are also referred to as functions. I've found however that life becomes easier when you always just refer to methods as methods, and reserve the word functions for objects that implement functional interfaces.

Object that implements a functional interface or Object of a class that implements a functional interface?


The two mean the same thing in object oriented programming.

This can be used only in case of functional interface but while trying to use it how to relate it to the functional interface.


I don't know what you mean. Can you reformulate your question?
 
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 don't know what you mean. Can you reformulate your question?






   lines.methodX(line -> Arrays.asList(line.split(" ")));



For the above kind of code. We can pass the function to the methodX , only after verifying something about methodX. What is it that we need to verify about methodX that it can function as parameter.  Do we have to next go to the  documentation of methodX and see whether it is written as methodX(func) instead of regular method like  something like methodX(String a, int b). Is that all ?
 
Stephan van Hulst
Saloon Keeper
Posts: 15484
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can assign the result of a lambda expression to any variable, field or method parameter if their type is an interface with exactly one abstract method. Such an interface is called a functional interface.

Take a look at the Comparator interface: Even though it has many static and default methods, it has only one abstract method: public int compare(T o1, T o2). That means that you can use a lambda expression or method handle whenever a variable, field or method parameter of this type appears:

An easy way to find out if an interface of the Java standard API is functional without having to check all its method declaration is seeing if it is annotated with @FunctionalInterface. If you look at the Comparator documentation again, you will see that it reads:
 
Campbell Ritchie
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Stephan van Hulst wrote:. . . seeing if it is annotated with @FunctionalInterface. . . .

That doesn't always work. This well‑known interface from GUI event handling is a functional interface, but isn't tagged with the @FunctionalInterface annotation.
 
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
So that means there are only limited situations where Functional programming can be used in Java. We can use functional programming Only in the cases where a method can take an instance of functional interface as the argument.
 
Campbell Ritchie
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Not at all.

You can easily write a method as a pure 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
Is there any example in java where functional programming is implemented apart from the cases of functional interface?
 
Campbell Ritchie
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Look through our functional forum. Find Pierre‑Yves Saumont's book.
 
Stephan van Hulst
Saloon Keeper
Posts: 15484
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:So that means there are only limited situations where Functional programming can be used in Java. We can use functional programming Only in the cases where a method can take an instance of functional interface as the argument.


No, you can only call higher order functions when methods accept a functional interface.

Campbell Ritchie wrote:Not at all.

You can easily write a method as a pure function.


I think what Campbell means by this is that you're confusing Functional Programming with calling 'higher order functions'. Being able to pass functions to higher order functions is only a small part of Functional Programming.

If you never reassign variables, and never call methods that have side-effects, such as setters, then you would be performing Functional Programming in Java, regardless of whether you use higher order functions or not. If we make an exception for System.out.println(), the following is a fully functional program:

The getFactorial() method has no side-effects: It doesn't modify any fields or variables, it doesn't call any other methods with side-effects and its output depends solely on its input.
 
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.

I think I had got confused between terms functional programming and lambda expressions. Functional programming is a broader term. A program where just a static method is called from the main method is also a function programming program.

Lambda expressions are limited only to the cases where Interfaces (functional ones)  are involved and passing of their instance to a method is to be replaced with passing of  a lambda expression to the method.
 
Campbell Ritchie
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Monica Shiralkar wrote:. . . Interfaces (functional ones)  are involved and passing of their instance to a method is to be replaced with passing of  a lambda expression to the method.

Not quite. The λ expression creates an instance, but by a different route from that for creating an anonymous class. Try it out in JShell:-

Campbell's JShell wrote: [campbell@localhost ~]$ jshell
|  Welcome to JShell -- Version 9.0.4
|  For an introduction type: /help intro
. . . .
ToIntFunction<String> lengthGetter = s -> s.length();
lengthGetter ==> ???
. . . .
jshell> ToIntFunction<String> lengthGetter = new ToIntFunction<>()
  ...> {
  ...>    @Override public int applyAsInt(String s)
  ...>    {
  ...>       return s.length();
  ...>    }
  ...> };
lengthGetter ==> ???

Note that I have corrected the indentation, and omitted quite a lot of the output.
 
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.   Let me correct it. Lambda expressions are limited only to the cases where Interfaces (functional ones)  are involved and passing of their instance to a method is taken care of implicitly in the background and from our side what we have to do instead is to pass the function (lambda  expression) to the method as argument instead of we ourself passing the instance of functional interface as argument to the method.  
 
Stephan van Hulst
Saloon Keeper
Posts: 15484
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I don't know what you by passing an instance implicitly.

When you evaluate a lambda expression it returns an object that represents a function. When you evaluate the new operator on an anonymous type declaration that implements a functional interface, it also returns an object that represents a function. In either case, you explicitly pass that object to a method.

Look again at Campbell's examples:


Whether you use the new operator or a lambda expression, they're both expressions that return an object. Whether you assign that object to a local variable first, or pass it directly to a method parameter doesn't matter and does the same thing in both cases.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic