1. Functional Programming
1) concept
Functional programming is a programming model that considers computer operations to be calculations of functions in mathematics and avoids the concept of state and Variable. Wiki
We know that object is the first type of object-oriented, then functional programming is the same, the function is the first type of functional programming. In object-oriented programming, We transmit the object, that in functional programming, we have to do is to pass the function, and this, as a term, we call him the Higher-order Function. Fei Lin Sha
2) Features
- Calculations are treated as functions rather than directives
- Pure functional Programming: No variables required, No side effects, simple testing (the result is the same each Time)
- Supports Higher-order functions with simple code
2. Python supports functional programming
- Not purely functional programming: allowed variables
- supports higher order functions: functions can also be passed in as arguments (variables)
- Support Closures: the ability to return functions with closures
- Limited support for anonymous functions
3. Higher-order functions in Python
1) Custom High-order functions (functions as Parameters)
Import Math def add (x, y, f): return F (x) + f (y)print add (9, math.sqrt)
2) Built-in High-order functions
format: map (f, lst)
F is the function name, can be a built-in function, or it can be a custom function
The LST is a sequence to be processed, and the type can be list,tuple and string, and the sequence can be
function: Each element in LST performs the operation of the F function
For example: assume that the user entered the English name is not standardized, not according to the first letter, followed by the letter lowercase rules, Please use the map () function, A list (including some nonstandard English name) into a list containing the English name of the specification: [' Adam ', ' LISA ', ' Bart '] output: [' Adam ', ' Lisa ', ' Bart ']
def format_name (s): return s[0].upper () +s[1:].lower ()print map (format_name, ['Adam' 'LISA'barT')
Functional Programming & High-order functions in Python map reduce filter and sorted