• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Knute Snortum
  • Bear Bibeault
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Frits Walraven
  • Carey Brown
  • Tim Holloway

determining that method of which functional interface does a lambda expression

 
Ranch Hand
Posts: 1115
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A lambda expression implements the method of a functional interface. By seeing the code how to know the name of this functional interface. By seeing the code before java 8, the interface name could be seen in the code but not in java 8 syntax.

thanks
 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

A lambda expression implements the method of a functional interface.


Actually, this is not true: lambdas can implement a functional interface, but often they are used as an Anonymous classes. SO you often can't tell what it implements.
E.g. this article explains this. In short, see this snippet:

 
Sheriff
Posts: 21758
102
Eclipse IDE Spring VI Editor Chrome Java Ubuntu Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Ivan Pronin wrote:

A lambda expression implements the method of a functional interface.


Actually, this is not true: lambdas can implement a functional interface, but often they are used as an Anonymous classes. SO you often can't tell what it implements.


It is true. Anonymous classes and lambdas are both techniques to implement a functional interface. The main difference is that anonymous classes aren't bound to only functional interfaces but can implement any interface with any number of methods (including zero).

I'd say the only way to determine the functional interface type is by looking at the context. What interfaces does a method accept, etc. IDEs can help you a lot with that.
 
Monica Shiralkar
Ranch Hand
Posts: 1115
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob Spoor

I'd say the only way to determine the functional interface type is by looking at the context. What interfaces does a method accept, etc. IDEs can help you a lot with that.



Thanks. Yes, using Eclipse IDE I could know determine that method of which is the Functional interface  does a lambda expression implement. My doubt is about code readability and easy of understanding what the code does. In java 7 code if someone looks at an existing java code, by seeing the code he will come to know this information that method of which Functional interface does a lambda expression implement. This interface name will be there in the code itself. In java 8 the developer has to take this code into Eclipse and then check this interface. There is more effort involved to understand what an existing java 8 code actually does (unless we take this to eclipse).
 
Marshal
Posts: 24594
55
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't agree. When I see code like



I don't find myself confused by the fact that I don't know what method of what interface that "some code" is implementing. I understand perfectly well what the code does without having to know that at all.
 
Monica Shiralkar
Ranch Hand
Posts: 1115
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Clapham says

I understand perfectly well what the code does without having to know that at all.



In case of below java 7 code, I understand that to the reduceByKey method we have to pass an object of Class which Implements the call method of  Function2 Interface.

JavaRDD<String> lines = sc.textFile("hdfs://log.txt");

// Map each line to multiple words
JavaRDD<String> words = lines.flatMap(
 new FlatMapFunction<String, String>() {
   public Iterable<String> call(String line) {
     return Arrays.asList(line.split(" "));
   }
});

// Turn the words into (word, 1) pairs
JavaPairRDD<String, Integer> ones = words.mapToPair(
 new PairFunction<String, String, Integer>() {
   public Tuple2<String, Integer> call(String w) {
     return new Tuple2<String, Integer>(w, 1);
   }
});

// Group up and add the pairs by key to produce counts
JavaPairRDD<String, Integer> counts = ones.reduceByKey(
 new Function2<Integer, Integer, Integer>() {
   public Integer call(Integer i1, Integer i2) {
     return i1 + i2;
   }
});

counts.saveAsTextFile("hdfs://counts.txt");




However in the below java 8 equaivalent code of the same, I do not come to know about call method or Funciton2 interface (even the names of these are not in the code). I can see these when I take code to Eclipse and check.




thanks..
 
Saloon Keeper
Posts: 10302
217
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And why should it matter that there is an interface called Function2, or that it has a call() method?

You can tell from looking at the Java 8 code that this particular invocation of reduceByKey() requires a function that takes two Integers and returns an Integer. The name of the functional interface is simply not interesting. If you DO need to know about the interface, because you want to look up the contract of the call() method, you can just go to the API documentation, but you would have to do that for the Java 7 code as well.
 
Monica Shiralkar
Ranch Hand
Posts: 1115
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

requires a function that takes two Integers and returns an Integer



Requires a function OR requires an object of class which implements a particular method of a particular interface?
 
Marshal
Posts: 64630
225
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That depends on the signature of the method using the λ. As you doubtless already know, you can create λs inside method calls or to replace instance creation expressions.You will already know that − is unreliable in such comparisons, but it shou‍ld be all right for small non‑negative ints. You will surely be familiar with comparing objects, as described in the Java™ Tutorials.
In that case, you know you are creating a Comparator instance because that is how you declared it. You also know what the definition of a functional interface is, and about the @FunctionalInterface annotation. The API documentation for @FunctionalInterface is also very helpful. So you are bound to know how to work out which of the methods of Comparator<T> is abstract: if not, have a look at its Java7 documentation where that is easier to see, without all the default methods. So: to work out which method of an interface is replicated by a λ, work out which of its methods is abstract. In the case of Comparator<T>, that is easy because its documentation says it is a functional interface and it is tagged with that annotation. Once you have worked out that Comparator<T> is a functional interface, and you know the details of List#sort(Comparator), you will know which interface you are instantiating and which method you are calling if you write something like:-Now, I said, “…which of its methods is abstract,” and that w‍ord “is” implies one method. Let's have some AWT/Swing:-Now, MouseListener has five abstract methods, so as hard as you try, you cannot turn it into a functional interface. If you succeeded, the compiler could never work out which method you were trying to implement as a λ, and it couldn't work out what to do with the other four methods.
On the other hand, what about this?If you look at ActionListener, you will find it is a functional interface. It might not have the annotation described in the documentation, but it has one abstract method. I am sure you know how many non‑abstract methods it has already. Because it has only one abstract method, that is the method which is replicated in the λ.

So, to answer the question in the threa‍d title:
  • 1: Read the documentation of the method you are calling to identify the interface you are instantiating, as Stephan has told you.
  • 1½: If you are declaring a variable with a type, that will tell you which interface you want.
  • 2: Look in that interface and see which its one abstract method is.
  • You have already been told these things. You cannot tell what the interface name being implemented is from the code; you have to look in the documentation, but as Paul C says, you don't actually need to know that. In the case of this sort of JavaFX code:...you shou‍ld have no dofficulty working out what the λ means without knowing you are implementing EventHandler<ActionEvent>.
     
    Monica Shiralkar
    Ranch Hand
    Posts: 1115
    4
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    as Paul C says, you don't actually need to know that



    That is the doubt which I have.

    The reason why I need to know that is that it is which method of which functional interface is that if I read an existing code like below and have to implement another code somewhat similar to it:







    then I need to know that I have to implement it as).This I by looking at  call method of the Function2 interface to know what parameters it take what it returns . Now in the java 8 code there is no mention of the name Function2 or the call method.

    I mean I can easily take this code to eclipse and know these things but it the understandability of the code based on looking at it not reduced in java 8? That is my question.

    thanks




     
    Campbell Ritchie
    Marshal
    Posts: 64630
    225
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Where does the RDD class come from? What does it say in its documentation? Similarly for the Tuple class.
     
    Monica Shiralkar
    Ranch Hand
    Posts: 1115
    4
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    Where does the RDD class come from? What does it say in its documentation? Similarly for the Tuple class.



    It comes from the below packages:

     
    Campbell Ritchie
    Marshal
    Posts: 64630
    225
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    And what does the documentation for those methods say?
     
    Monica Shiralkar
    Ranch Hand
    Posts: 1115
    4
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Scala 2 tuple:

    Scala tuple combines a fixed number of items together so that they can be passed around as a whole. Unlike an array or list, a tuple can hold objects with different types but they are also immutable.

    JavaRDD:

    Class JavaRDD<T>

    java.lang.Object
    org.apache.spark.api.java.JavaRDD<T>
    All Implemented Interfaces:
    java.io.Serializable, JavaRDDLike<T,JavaRDD<T>>

    public class JavaRDD<T>
    extends java.lang.Object
     
    Campbell Ritchie
    Marshal
    Posts: 64630
    225
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    The documentation for the methods shou‍ld tell you which types they take as their parameters.
     
    Monica Shiralkar
    Ranch Hand
    Posts: 1115
    4
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    The documentation for the methods shou‍ld tell you which types they take as their parameters.



    Yes and in case of java 8 code I posted above the method name for call method can be seen nowhere in the code unless we take it to Eclipse and check.

    whereas in java 7 equivalent code I posted above it clearly shows there is such a method by name 'call'.

    thanks.
     
    Saloon Keeper
    Posts: 3291
    146
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I just had a look at the Apache JavaPair class documentation. Now, there is no explaining what this class is supposed to do (seems like it contains many utility methods), but if you look at the 'reduceByKey' method, you see that it is an overloaded method, In this case it says:

    Looking at the interface Function2, you see that the abstract method is

    It is exactly as in java 8, although there it is called either BinaryOperator or BiFunction.

    What you example is doing is creating a frequency table of all the words. In java 8, you would do something like

     
    Campbell Ritchie
    Marshal
    Posts: 64630
    225
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    There shou‍ld be documentation from the suppliers of the RDD class you are using. Have you looked for that? It took me about half a minute to find it with a search.
     
    Monica Shiralkar
    Ranch Hand
    Posts: 1115
    4
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    sorry if I could not express my question correctly in original post.
    Yes I saw the documentation and also took the code to eclipse. I was able to correctly run the java 8 program as well as the java 7 program. My doubt was not regarding unable to run the program.Rather it was regarding whether the java 8 code is bit lesser understandable (without reading documentation or taking the code to Eclipse) because of no mention of e.g the call method (of Function2 Interface) unlike in the code for In java 7 where without taking code to eclipse we could see which method of which interface was being implemented.
     
    Stephan van Hulst
    Saloon Keeper
    Posts: 10302
    217
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Yes. Our point is that knowing the type and knowing the method that are being called doesn't matter. In your first example, there's absolutely no extra information you would get from knowing that the type is PairFunction, and that the method is named call().
     
    Monica Shiralkar
    Ranch Hand
    Posts: 1115
    4
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    If someone reads the below java 7 code he will come to know what is happening (even without taking it to Eclipse ). He will understand that there is a call method of Function2 interface and its implementation class should pass so and so parameters to it and so and so return type:



    But if I see the below equivalent code of it, one come to know that piece of information until and unless one takes it to Eclipse or checks the documentation:



    thats my only point and doubt. (I am perfectly able to run both the codes successfully)
     
    Campbell Ritchie
    Marshal
    Posts: 64630
    225
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I think that is a case of learning to read the Java8 syntax. Java8 is now three years old. Your Java7‑style code is less than half the code, the call() method, but the second is the whole of the code. There are a few things in the Java8 code which are not clear, for example what sc is.
    And another thing: I looked at the Apache documentation yesterday and thought it was not well written, I am afraid. That goes to show you how important well&#c2011;chosen identifiers are: you can guess what the saveAsTextFile method does without having to read its documentation.
     
    Consider Paul's rocket mass heater.
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!