Why - in Java 1.8 - is the function <V, R> used, and not the function <R, V>?

The order seems odd, because in normal Java, the return type is always set first. How in:

public static double sum(Iterable<Number> nums) { ... } 

Why then in the Function and BiFunction classes was a choice made to indicate them the other way around? How in:

 interface Function<T,R> interface BiFunction<T,U,R> 

I am not asking here for an opinion on which is better, but specifically:

a) Is there any technical or other (non-stylistic) benefit in preferring one order over another? Or is it an arbitrary choice?

b) Does anyone know of any documented explanation or any stated reason from an authoritative source, why was one chosen differently?

In addition, the order seems even stranger if it is expanded to higher values. For example, the hypothetical function QuadFunction:

 interface QuadFunction<A,B,C,D,R> { ... } 

(During recording, the highest arity in library 2 โ€” that is, BiFunction.)

See: http://download.java.net/jdk8/docs/api/java/util/function/package-summary.html

+6
source share
5 answers

This should be consistent with previous existing designations.

The function of mathematical integer division extends to rational numbers:

 (\): I x I -> Q 

Functional programming version above (e.g. Haskell, Ocaml)

 division :: Integer -> (Integer -> Rational) or division :: Integer -> Integer -> Rational 

All three say that "the division function takes two integers and returns a rational number." It is back, in a functional paradigm, to tell your returns first. C taught us to say "we return a rational number in a division function that takes two integers" (ex float division (int a, int b) {}).

In Java, your return type is to the left of the methods, because Java wants to look like C. C designers thought "int main (int argv, char * argv [])" looked better than "main (int argv, char * argv [] ) int ". When writing code, at least for me, I most often donโ€™t know how the method will return before I find out what it needs. (edit 1: and we will write strings such as String s = removeSpaces (textLine), so the left variable corresponds to the left to left)

In C #, func looks just like a Java 8 function.

+13
source

I assume it is more intuitive for a chain of methods, which could be a typical use case for lambdas, i.e.

 IntStream.range(1, 10).map(Ints::random).filter(x -> x % 2 == 0) 

So, the sequence method is read from left to right, and lambdas go left-right. So why not have type parameters going from left to right?

The escalation of this is a little further - the reason may be that English is read from left to right .:-)

UPDATE

I was very surprised to learn that this is something that happens to the mathematician of modern Arabic notation :

Latin complex numbers

latin complex numbers

Arabic compound numbers

arabic complex numbers

In this example, the Arabic notation in each mirror is char. It can be traced under the sign of the angle and I (the imaginary unit) char - in both cases it has a point. The related wiki article also shows an example of the lim arrow reversed (compared to the arrow arrow in Java 8 format). This could mean that Arabic Java, if it had ever been developed, would have looked different. :-)

Disclaimer: I have experience in mathematics, but I had no idea about Arabic notation when I answered this question.

+6
source

In normal procedural and OO programming, functions / methods usually take a list of parameters and return some result:

 int max(int num1, int num2) 

When rewriting function signatures as a callback (for example, for parallel or asynchronous processing), it has long been customary to convert the signature by adding a callback in the last parameter:

 void maxAsync(int num1, int num2, Callback<int> callback) // pseudo-Java 

The current example of this template can be found in the processing of the GWT RPC .

This style arose from the Lisp style of languages โ€‹โ€‹with the so-called continuation-skipping style , where functions are bound, passing the function to the function as a parameter. Since in Lisp, arguments are evaluated left to right, a function that consumes values โ€‹โ€‹should be at the end of the list. This arrangement was adopted by imperative languages โ€‹โ€‹to ensure continuity, and because at the end of the parameter list it was traditional to adhere to additional optional parameters (Boolean flags, etc.).

+4
source

This is a clear intention to make it more convenient for functional style programming in Java. Now, in math, a function is usually written as

 f: A -> B 

(i.e., a function from As to Bs). This also matches notation in functional languages, Scala, and existing functional libraries for Java.

In other words: this is the right thing.

Note that the functional interface is not a method, and the method is not a functional interface, so it is not clear which syntax of the former is related to the latter.

+4
source

Just my opinion: look the same as Function in guava. I believe that the order, on the contrary, will cause a lot of confusion.

http://docs.guava-libraries.googlecode.com/git/javadoc/com/google/common/base/Function.html

0
source

Source: https://habr.com/ru/post/957188/


All Articles