One aspect that is often overlooked in the Java language is that it is categorized as an imperative (imperative) programming language. Imperative programming, although fairly popular due to its association with the Java language, is not the only programming style available, nor is it always the most efficient. In this article, I'll explore adding different programming methods--functional programming (FP)--in Java development practice.
Imperative programming is a method of using program state to describe the calculation. Programmers using this paradigm change the state of the program with statements. That's why a program like Java is made up of a series of commands (or statements) that the computer executes. Functional programming, on the other hand, is a programming style that emphasizes the computation of expressions rather than the execution of commands. An expression is composed of a function combined with a base value, similar to calling a function with a parameter.
This article describes the basic features of functional programming, but focuses on two elements that are particularly suitable for the Java development framework: Closures and High-order functions. If you've ever used an agile development language like Python, Ruby, or Groovy, you may have encountered these elements. Here you will see what happens when you use them directly in the Java development framework. I'll start with a short, conceptual overview of functional programming and its core elements, and then use the usual programming scenarios to show how the structured way to use closures and higher-order functions can benefit Java code.
What is functional programming?
In the oft-cited paper "Why Functional Programming Matters", author John Hughes explains that modularity is the key to successful programming, and functional programming can greatly improve modularity. In functional programming, programmers have a natural framework to develop smaller, simpler, and more generic modules, and then combine them. Some of the basic features of functional programming include:
Supports closures and High-order functions.
Supports lazy computing (lazy evaluation).
Use recursion as a mechanism for controlling processes.
Enhanced referential transparency.
No side effects.
I'll focus on using closures and higher-order functions in the Java language, but start with an overview of all the features listed above.
Closures and higher-order functions
function programming supports functions as the first class object, sometimes called a closure or an imitation function (functor) object. In essence, closures are objects that act as functions and can operate like objects. Similarly, the FP language supports higher-order functions. A higher order function can use another function (indirectly, an expression) as its input parameter, and in some cases it may even return a function as its output parameter. These two structures combine to make modular programming in an elegant way, which is the best benefit of using FP.
Command-type programming
Imperative programming the name is derived from the imperative tone (imperative mood) of a natural language (such as English), in which the order is declared and followed. In addition to the Java language, C and C + + are two other widely used, command-style high-level programming languages.
Lazy calculation
In addition to the concepts of higher-order functions and imitation functions (or closures), FP also introduces the concept of lazy computing. In lazy calculations, an expression is not evaluated immediately when it is bound to a variable, but rather when the evaluation needs to produce the value of an expression. Deferred computations allow you to write functions that potentially generate infinite output. You do not need to worry about out-of-memory errors caused by infinite computations because you do not compute the values that are required for the rest of the program. An example of lazy computing is a function that generates an infinite Fibonacci list, but the calculation of the nth Fibonacci number is equivalent to simply extracting an item from a list of possible infinity.
Recursion
FP is also characterized by recursion as a mechanism for controlling the flow. For example, the Lisp-processed list is defined as having a child list behind the header element, which makes it natural for the smaller child list to be recursively recursive.
About implementing libraries
I use the library provided by the Apache Commons functor project to build the examples used in this article. The Apache Commons functor Library includes a large number of basic constructs that can be reused in complex usage scenarios involving closures and high-order functions. Of course, you can use different implementations (such as Java Generic Libraries, Mango, or Generic algorithms for Java) without affecting the concepts discussed and demonstrated in this article, although you must download and use the Apache Commons functor Library to demonstrate the examples here.