Functional programming and function closure of Python

Source: Internet
Author: User

Security Code: Indifferent Childe


Python-Functional Programming:

Functional Programming:

Also known as functional programming, is a programming paradigm, which is the implementation of the function can be passed to another function as a parameter;

It treats the computer operation as a mathematical function calculation, and avoids the state and variable data;

The most important foundation of a functional programming language is the lambda calculus, and the function of the lambda calculus can accept functions as inputs and outputs

Python supports a limited functional programming function:

Filter (FUNC,SEQ):

Invokes a Boolean function, Func, to iterate through the elements in each SEQ, and returns a sequence of elements that enable Func to return a value of true

In [1]: Def A (x): ...: If x >: ...: Return True ...: else: ...: return False ...: In [2]: L1 = [1,2,3,4,20,21,25,40,50,32,47]in [3]: Filter (A,L1) out[3]: [21, 25, 40, 50, 32, 47]

Filter () is a filter:

Filter () invokes the given Boolean function for each element of the known sequence;

In the call, elements that return a value other than 0 will be added to a list

650) this.width=650; "src=" Http://s2.51cto.com/wyfs02/M02/8B/DA/wKioL1hbN5vidLPzAABQNMAdVDI008.jpg "title=" Filter.jpg "width=" "height=" 333 "border=" 0 "hspace=" 0 "vspace=" 0 "style=" width:700px;height:333px; "alt=" Wkiol1hbn5vidlpzaabqnmadvdi008.jpg "/>


Map (FUNC,SEQ1[,SEQ2 ...]) :

The function func acts on each element of the given sequence (s) and provides the return value with a list;

If Func is none,func as an identity function, returns a list of n tuples containing the collection of elements in each sequence

In [1]: L1 = [0,1,2,3,4,5,6]in [2]: L2 = [' Sun ', ' Mon ', ' Tue ', ' Wed ', ' Thu ', ' Fri ', ' Sat ']in [3]: Map (NONE,L1,L2) out[3]:[(0, ' S Un '), (1, ' Mon '), (2, ' Tue '), (3, ' Wed '), (4, ' Thu '), (5, ' Fri '), (6, ' Sat ')]
In [4]: L1 = [0,1,2,3,4,5,6]in [5]: L2 = [' Sun ', ' Mon ', ' Tue ', ' Wed ', ' Thu ', ' Fri ', ' Sat ']in [6]: Def a (x, y): #这里的函数参 Number of numbers to match the number of sequences in the map function ...: return x*2,y*2 ...: in [7]: Map (A,L1,L2) out[7]:[(0, ' Sunsun '), (2, ' Monmon '), (4, ' tuetue ') , (6, ' wedwed '), (8, ' Thuthu '), (Ten, ' Frifri '), (A, ' Satsat ')]

Map () is a mapper;

Map () "Maps" a function call to the corresponding element of each sequence and returns a list with all the returned values. The white point is that it can integrate elements from the same position of different sequences into a tuple after being processed by the Func function, resulting in a tuple list

Map with a single queue () as shown in:

650) this.width=650; "src=" Http://s2.51cto.com/wyfs02/M01/8B/E0/wKiom1hbZIPQj98mAABGdO_mNic651.jpg "title=" Map.jpg "alt=" Wkiom1hbzipqj98maabgdo_mnic651.jpg "/>

Map with multiple queues () as shown in:

650) this.width=650; "src=" Http://s1.51cto.com/wyfs02/M01/8B/E0/wKiom1hbY6bBTigeAABmT2xwWRk646.jpg "title=" Map2.jpg "width=" "height=" 438 "border=" 0 "hspace=" 0 "vspace=" 0 "style=" width:700px;height:438px; "alt=" Wkiom1hby6bbtigeaabmt2xwwrk646.jpg "/>


Reduce (Func,seq[,init]):

The two-tuple function acts on the elements of the SEQ sequence, each time carrying a pair (previous results and the next sequence element), successively the existing results and the next value on the resulting subsequent results, and finally reducing our sequence to a single return value; If the init value is given, The first comparison will be init and the first sequence element instead of the sequence's first two elements

White point reduce is the realization of folding function

In [9]: L1 = [0,1,2,3,4,5,6]in]: Def a (x, y): ...: return x + y ...: in [All]: Reduce (A,L1) #返回所有参数之和Out [One]: 21In []: Reduce (a,l1,10) #返回所有参数 + initial value of out[12]: 31


Python function closures:

Closures are called lexical closure (lexical closures). Refers to the whole of functions and related environments.

Closure refers to the whole of an inner function and the contents of the Environment (outer function).

Closures are just like functions in form and expression, but in fact closures themselves are not functions.

A closure can be interpreted as a function in a nested environment, if a variable in the scope of an inner function is referenced in the outer layer function, then the inner function can still use the variable referenced in its outer function when it returns, and this variable forms the environment that the inner layer function can use.

in [1]: def func1 (x):              #外层函数    ...:     def  Func2 (y):         #内层函数    ...:          return y ** x   ...:      RETURN FUNC2   ...:IN [2]: F4 = FUNC1 (4) In [3]: type (F4) Out[ 3]: FUNCTIONIN [4]: F4 (2) out[4]: 16in [5]: f4 (3) out[5]: 81 
in [6]: def startpos (m,n):          #象棋起始位置    ...:     def newpos (x, y):        #象棋新位置    ...:          return  "the old position is  (%d,%d), And the new position is   (%d,%d). " %  (m,n,m+x,n+y)    ...:     return newPos    ...: In [7]: action = startpos (10,10) in [8]: action (+) out[8]:  ' The  old position is  (10,10),and the new position is  (11,12). ' In [9]: action = startpos (11,12) in [10]: action (3,-2) Out[10]:  ' The old  position is  (11,12),and the new position is  (14,10). ' 

When the variables in the outer function are modified, the inner layer function is affected accordingly, so that the outer function provides a running environment for the inner layer function, which is called the closure.


This article from "Indifferent bo" blog, please be sure to keep this source http://itchentao.blog.51cto.com/5168625/1885110

Functional programming and function closure of Python

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.