On functional programming of Java 8

Source: Internet
Author: User
Tags data structures mathematical functions new features

There has been much discussion about "Java 8 brings functional programming to Java", but what is the true meaning of this statement?

This article discusses the function, what it means to a language or a programming approach. Before replying to "How is Java 8 Functional programming", let's look at the evolution of Java, especially its type system, and we'll see the new features of Java 8, especially how lambda expressions change the landscape of Java and provide the main advantages of functional programming style.

What is a functional programming language?

The core of a functional programming language is its handling of code in a way that handles data. This means that the function should be the first level (first-class) value, and can be assigned to the variable, passed to the function, and so on.

In fact, many functional languages go farther than this, and the calculations and algorithms look more important than the data they manipulate. Some of these languages want to detach program state and function (in a seemingly antagonistic way, using object-oriented language, which usually ties them more closely).

The Clojure programming language is one such example, although it runs on a class based Java Virtual machine, Clojure is essentially a functional language and does not advertise classes and objects directly in high-level language source programs (although it provides good interoperability with Java).

The following shows a Clojure function, which is used to process logs, is a first-class citizen (first-class citizen), and does not need to bind a class to exist.

(Defn build-map-http-entries [Log-file]
 (Group-by:uri (scan-log-for-http-entries log-file))

Functional programming is most useful when a program written in a function always returns the same output for a given input, regardless of the other states in the program, and does not have other effects or changes any program state. They behave the same as mathematical functions, and sometimes the functions that follow this standard are called "pure" functions.

The great benefit of pure functions is that they are easier to infer because their operations do not depend on the external state. Functions can be easily combined, which is common in developer workflow styles, such as the REPL (Read, Execute, Print, Loop) style that is common in Lisp dialect and other languages with strong functional traditions.

Functional programming in non-functional programming languages

A language is not a function is not an either-or state, in fact, the language exists on the atlas. At the very end, it is essentially mandatory functional programming, which usually prohibits mutable data structures. Clojure is a language that does not accept variable data.

However, there are other languages that are usually programmed in a functional way, but the language does not enforce this. Scala is an example of a mix of object-oriented and functional languages. Allows a function to be a value, for example:

Val sqfn = (x:int) => x * x

Also preserves class and object syntax that is very close to Java.

Another extreme, of course, is the possibility of functional programming using completely non functional languages, such as C, as long as the appropriate programmer guidelines and conventions are maintained.

With this in mind, functional programming should be seen as a two-factor function, one of which is related to programming languages and the other is a program written in that language:

1. To what extent does the underlying programming language support or enforce functional programming?

2 How does this particular program use the functional features provided by the language? Does it avoid non functional attributes, such as mutable states?

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.