In the original article, Lambda expressions must be included in JDK 7, and the full text also focuses on this knowledge, the author believes that the concept of functional programming will also appear in JDK 7.
Lambda expressions
Lambda expressions are not a new concept. They have been around for 30 years since Alonzo Church proposed the concept of Lambda calculus. Since then, they have become an important feature of many functional programming languages, the most prominent is probably Lisp. Now Lambda expressions are among the first class citizens in functional programming languages. Lambda expressions are anonymous functions. For demonstration, there is a Python code segment that you may often see:
- List = [1, 2, 3, 6, 8,]
- Print filter (Lambda x: x * 2> 10, list)
- [6, 8]
The expression "Lambda x: x * 2> 10" is a Lambda function. It is anonymous at runtime and executed in the filter function. Similarly, you can assign the same expression to a variable for transmission or call the function itself:
- F = Lambda x: x * 2> 10
- Print filter (f, list)
- [6, 8]
- # Let's call f itself
- F (1)
- False
In Java, an anonymous internal class proves that Lambda expressions are anonymous. When a method is passed to them, they are common objects, such:
- File cwd = new File (".);
- System. out. println (cwd. list (new FileFilter (){
- Public boolean accept (File f ){
- Return f! = Null & f. getName (). endsWith (". java ");
- }
- }));
Lambda expressions can now be simply treated as an anonymous function. In Java, this may mean "Callback", true recursion and other functions, or a Comparator (Comparator) the day when the interface filters the set will no longer be enough. If they are completed in JDK 7, we expect to see a more dynamic language.
Function Type
As mentioned above, Lambda expressions introduce another functional programming concept: function types. It only refers to a function as an object, such as a String or BigDecimal, which allows you to pass it to other functions like other types.
Closure
You should have heard of the concept of closure. It is another very simple functional programming concept. It allows a function to include another function, and can reference external variables in internal functions, these variables are called "Free variables" because they are neither closure parameters nor local variables.
In fact, if you have read articles about JDK 7, you will find that the word closure appears more frequently than Lambda expressions, because they usually appear together, when a Lambda expression accesses an external variable, it becomes a closure. According to the definition, a closure is also a Lambda expression.
High-order functions
A high-order function is a function that gets or returns another function. In the Python code example at the beginning of this article, you should have seen the filter function, which is a high-order function, because its first parameter is a function, this function is used to test the content of each element in the list (second parameter.
Partial function
It is best to interpret a partial function as a "chained" Lambda expression. For a function that accepts multiple parameters, a partial function is a function conversion process so that each parameter is passed to the function, then return according to the original function call.
- // "Mul" is function that takes two arguments; both ints
- Mul (5). (5); // returns 25
Other good functional programming concepts
Other functional programming concepts that I think are good:
◆ List parsing-Describe the syntax used to generate a List (set, ing, etc.) in a command line, such as "List <String> l = (for x in someOtherCollection) {x. someMethod () & x. another ();}"
◆ Tail recursion-call your own function at the end of the statement, and the compiler (or runtime environment) can recognize it, so the code can be significantly optimized.
What is the significance of functional programming?
Well asked, functional programming has many advantages. The most obvious difference is that the total amount of code is less. You don't need to implement interfaces for functions such as file name filtering and comparison. You can directly pass functions.
Summary
The world of programming is always filled with ideas and traditions of other new ages. Functional programming is just one of the best software development methods. It has its own advantages and disadvantages and may help you write better software. In any case, I believe that the Lambda expression project can be completed. Although it still has defects, it will bring fresh air to programming languages and be welcomed by developers.
Author: Alex Collins
Original article: Functional Programming Concepts in JDK 7
Original article address: