Jane said JAVA8 introduction of functional problems

JAVA8 added lambda calculus is an exciting new feature - although this new feature too late, the current mainstream development language, JAVA language appears to be the last functional thinking support.

 

Although a bit late, but better than nothing - Moreover, I think it's still possible to achieve, at least a little better than C ++ implementation (C ++ compiler can not automatically a good deal closures environment, but requires the programmer to code Specifies introduced into the lambda expression variables (capture list) - C ++ type system too rich, if not capture list, the compiler should not know the "value" capture by capture or "reference", and the JAVA type system is much simpler, do not have this problem).

 

Every time before I returned from Scala, Java, there is a sense of pain --Scala library collection designed for functional design, its level of abstraction than a collection of Java libraries that often Scala two or three lines of code can handle things in Java dozen lines of code to the job.

Now Java8 months (from the beta version of the time I just use openjdk8 a), the company go back to using jdk6 old-fashioned, and it is terribly painful - say jdk6 is not too old-fashioned, from the beginning of 2003 jdk5 prior to the release jdk7, java syntax never changed, and now the latest version of the company's use of jdk or jdk6-- although Jdk8 currently supported function syntax is still far Scala on simplicity, it would be leaps and bounds .

 

After jdk8 official release, the online article on jdk8 started up, it is basically a burst of applause. Personally, I also really like these changes, but because: to have a lot of people say that these benefits, and I do not need another augmented state; secondly jdk8 now is mostly a love of learning with friends for a good place to spend time naturally to understand, you do not need much to say.

 

So I am here to say that java might be a function of not thinking things ready, and its advantages, if there are a lot of people have mentioned, I am not here to say, if it comes less than, I was a little speak a few words, such as this one would rarely mentioned:

The new Stream interface to achieve a non-termination of operation are lazy evaluation, this design ensures that the overall operating efficiency of collection operations.

 

In the big data era, concurrent capacity will become the core competitiveness of the language, and the performance of concurrent programs functional language development are usually better than an imperative language, which is why the java this almost 20 years of language began to change the thinking of one of the reasons.

For example: Scala and Java is running the same language on the JVM, but Spark Scala Hadoop performance in relation to the development of Java developers have a 100-fold increase.

The figure comes in apache spark project website (logistic regression algorithm execution time comparison)

Such a large gap is hard to believe, but for now, this is the truth - the same JVM, only the differences in language can have such a difference in performance, according to what?

I think the reason not as good as the performance of Hadoop Spark is not so high-performance use Java to write things out, but there are some things so there is a lot of resistance in the development of high-performance concurrent programs in Java.

 

I do not have in-depth study of the internal mechanisms of Hadoop and Spark, so the language itself says only about Java8 currently remaining in the (possible) weakness.

Note: The following column first with twelve simple look, at a later date supplement.

 

First, there is empty.

null is a normal java code is common keywords, but functional thinking, the air does not make sense.

An object is empty, it means you can not call any of the methods of this object, and in the functional program, if a function can not match an object, usually also means that the function is over.

If an empty object will continue to function, it usually means that the null reference will be given new object.

jdk8 collection can also be seen in order to avoid empty frame and modified methods, such as:
the Map interface adds a method getOrDefault issue to try to circumvent the get method will return null;

Optional Jdk8 new class is also increased to match the analog functional language model - space pattern matching is not possible.

 

Second, the presence of the state variable.

An object, if it is variable, it means that the object concurrency problems.

But the object in java to make a type can not be changed, is a very troublesome thing (Imagine all the attributes of all plus a final), developed a variable state of the object is very easy.

While writing a function in line with purely functional requirements of the function you do not necessarily have to use an immutable state, but if all objects are immutable state, then the write will certainly meet the functional standards.

Mutable objects often means concurrent call this object will have problems, so we need a certain part of the object lock to circumvent the problem, and the lock is complicated by the deterioration of the most direct cause.

 

Third, no tail recursion optimization.

Scala and Erlang in such a functional style language, tail recursion optimization is already very common concept, but most of JAVA programmers have not heard of this concept (because there is no tail recursion optimization Java) - on tail recursion optimization can refer to my other article " on SNL language interpreters implement tail recursion optimization ."

Functional style of immutable state objects have a great impact on concurrency, but an imperative language used to write code that programmers will produce a very unaccustomed effect - no circulation.

Imagine: a language is not for no while, could be a disaster. But if you are familiar with the functional style, even if there is no cycle, we can achieve an equivalent function.

 

Fourth, consider upward compatible too.

java from jdk5 to the present jdk8, no increase in a reserved word, all the new syntax changes fully reflected in the code structure, with a lot of new ideas to achieve the library, which greatly affected the use and promotion of new ideas.

But the relationship between this and the performance is not too large (a good database design, performance is not bad), but also a little relationship - a lot of functional languages, some common data structure is the syntax of the language level, rather than the library level.

 

Fifth, no new more advanced threading model.

Some time ago to see a conversation is an interview with the author Yukihiro Matsumoto Ruby, presumably to say:

Q: What if the clock back, what would you do to change (for Ruby)?

A: remove the thread, the use of more sophisticated thread-based model of the actor.

 

First out more than a few, with the deepening of understanding of the future, may be added, deleted or modified.

 

The above points for ordinary java program, it is very common, but for the development of concurrent programs or applications functional thinking, will be different degrees of an obstacle.

 

Longer refer to my other article " pure functional thinking to write the next paragraph in Java8 wonderful program ," From here we can feel a lambda expression Java8 performance at a slightly more complex situation is odd how (chou) Pa (lou).

Reproduced in: https: //www.cnblogs.com/naturemickey/p/3900836.html

Guess you like

Origin blog.csdn.net/weixin_34162695/article/details/93461849
Recommended