08- Process for the functional

First, the programming paradigm

Refers to a programming paradigm programming routine, into object-oriented , functional , object-oriented , etc.

Second, process-oriented

1. Process for the basic concepts

The core for that process , process refers to the steps to resolve the problem, which is to do, after what

Process-oriented development process like an assembly line relates, it is a mechanical way of thinking

Pros: The complex issue processes, thus simplifying

Disadvantages: Scalability procedures difference

2. The process-oriented programming scenarios analytical thinking

  • Not all software requires frequent changes, such as scripting
  • Even a software requires frequent changes, nor does not mean that all the software components of the change are required together

Third, functional

The difference is that with the process-oriented, functional pay more attention to results

1. anonymous function

def for defining a named function

# 示例
def func(x,y)
	return x + y

Since there is well-known function, of course, there is an anonymous function that defines an anonymous function lambda

lambda x,y:x + y	# 相当于上述的func函数
# lambda x,y:x + y	此处的(x,y)为入口参数,x + y则为函数体

lambda functions simplifies the written form of a function definition, makes the code more compact, but even more a function definition is intuitive, easy to understand

Anonymous function is called once for temporary scene: more anonymous is used in conjunction with other functions

2. Application anonymous function

1) map functions

map () function will do the mapping functions provided according to a designated sequence

grammar
map(function,iterable,...)	
parameter
  • function- function
  • one or more sequences iterable-
return value

Iterator (python3)

List (python2)

2) filter function

filter () function is used to filter sequences, filtering out non-compliant elements, returns an iterator object

grammar
filter(function,iterable)
parameter
  • function - judging function
  • iterable - iterables
return value

Iterator object

3) reduce function

reduce () function parameter elements in the sequence will be accumulated

grammar
reduce(function,iterable,[initializer])
parameter
  • function- function, there are two parameters
  • iterable- iterables
  • initializer- Alternatively, the initial parameter
return value

The calculation result of the function

4) max function

max () function returns the maximum value of a given parameter, the parameter sequence may be

grammar
max(x,y,z,...)
parameter
  • x - numeric expression
  • y - numeric expression
  • z - refers to the number of expression
return value

The maximum value of a given parameter

5) min function

min () function with the max () function is a different, it returns the minimum value of a given parameter, the parameter sequence may be

grammar
max(x,y,z,...)
parameter
  • x - numeric expression
  • y - numeric expression
  • z - refers to the number of expression
return value

A minimum value of the given parameter

6) sorted function

sorted () function operation on all sorts of objects iterations

grammar
# 语法
sorted(iterable,key = None,reverse = False)
# iterable —— 可迭代对象
# key —— 主要用来比较的元素,只有一个参数,参数取自可迭代对象中
# reverse —— 排序规则,True为降序,False为升序(默认)
parameter
  • iterable - iterables
  • key - the main element for comparison, only one parameter, the parameter is taken from iterable
  • reverse - collation, True descending, False ascending order (the default)
return value

Reordered list

Guess you like

Origin www.cnblogs.com/zhuyouai/p/12577217.html