Anonymous functions (Anonymous function)
1. Anonymous function refers to a function that is not bound to an identifier. By literal means, it can also be understood as a function without a name.
2. Anonymous functions are usually:
1). As a parameter, it is passed to the higher order function.
2). The return value as a higher-order function.
3. Anonymous functions are common in functional programming languages, and in other languages that have first-class functions.
4. In some programming languages, anonymous functions are flagged by the keyword lambda. So, anonymous functions have also been called lambda Expressions (Lambda Express). For example, Python uses lambda to tag
def h (x): return Lambda y:x + y
5. An anonymous function is one of the nested functions that can access the variables of the outer function, that is, its non-local variables. This also means that anonymous functions need to implement closures.
6. Anonymous functions are a programming style. Anonymous functions are never the only way to solve a problem. Each anonymous function can be redefined as a well-known function and called the famous function.
7. Anonymous functions are commonly used in the curry scenario, in addition to the advanced functions mentioned above, closures.
Higher order functions (Higher-order function)
1. A function is called a higher order function and needs to satisfy at least one condition
1). Define one or more functions as parameters of their own
2). Returns a function as its own return value.
2. The above Python example h (x) is a higher order function. The standard function of the C language, Qsort, is also a higher order function.
Map
1. Map is a common high-order function that passes each element of a given data set into a given function and invokes it, returning the called data collection, with the element order intact. Often referred to as "Apply-to-all", that is, all calls.
2. The following is a square map application, implemented using scheme.
Define the square algorithm first
Square x = x * x
Then call map
>>> Map Square [12345]
The resulting results are [1, 4, 9, 16, 25].
In Java 8, Stream.map (func) was introduced to support the map feature. The code is as follows
list<integer> numbers = arrays.aslist (1, 2, 3, 4, 5); List<Integer> squrelist = Numbers.stream (). Map (i-i*i). Collect (Collectors.tolist ()); SYSTEM.OUT.PRINTLN (numbers); Output: [1, 2, 3, 4, 5] System.out.println (squrelist); Output: [1, 4, 9, +,]
Curry (currying)
1. Curry is a function conversion technique that converts a multi-parameter function into a function that requires only a single parameter for a column, where the function single parameter corresponds to the previous multi-parameter one by one.
function divide (x, y) { return x/ y} function Divior (d) { return function Span style= "color: #000000;" > (x) { return divide (x, D); }} var half = Divior (2 var third = Divior (3) // return third // return 8
2. In mathematical analysis, there are some analytical techniques that only support single-parameter functions, but useful functions usually define multiple parameters. Curry is the conversion of multi-parametric functions into some series of single-parameter functions. This technique is used for theoretical analysis and less for industrial production.
First-class functions (first-class function)
1. A programming language supports the first-class function, which means to use a function as a primary citizen, or as a type. Have the following features
1). You can pass functions as arguments to other functions
2). You can use functions as return values for other functions
3). You can assign a function to a variable or store it in a data structure.
2. The identifier of the function is a normal variable, the type is the function type, there is no other special state.
3. The first-class function is a precondition for functional programming style, because functional programming uses higher-order functions as a common method.
4. A function can be the value of a variable, then it is necessary to determine whether the two functions are equal. There are two main kinds of equality.
1). The connotation is equal (intensional equality). By comparing the source code of the function body, the two functions are judged to be equal.
2). Reference equality (Reference equality). All functions have a corresponding identifier that determines whether the function itself is equal by judging whether the identifiers of the two functions are equal. So, two of the methods that are defined separately, even if their source code is a glance, are considered unequal. Most languages use reference equality to determine whether the functions are equal.
5. Javascript basically supports the first-class function in its entirety.
The closures introduced in Java 8 can only obtain invariant non-local variables. There is no function type in Java.
Resources
Anonymous function, Wikipedia
Higher-order function, Wikipedia
Map (Higher-order function), Wikipedia
curring, Wikipedia
First-class function, Wikipedia
[Java] Functional programming related concepts-NOTE 2