13.函数式编程:匿名函数、高阶函数、装饰器

# def add(x,y):
#     return x + y
# print(add(1,2))  # 3

# 匿名函数
# lambda表达式
# f = lambda x,y:x + y
# print(f(1,2)) # 

# 三元表达式
# x = 2
# y = 1
# r = x if x > y else  y
# print(r) # 2

# map

# list_x = [1,2,3,4,5,6,7,8]
# def square(x):
#    return x * x

# 方法1
# for x in list_x:
#     square(x)

# 方法2
# r = map(square,list_x)
# print(r)        # <map object at 0x029F31F0>
# print(list(r))  # [1, 4, 9, 16, 25, 36, 49, 64]

# r = map(lambda x:x*x,list_x)
# print(list(r))  # [1, 4, 9, 16, 25, 36, 49, 64]

# list_x = [1,2,3,4,5,6,7,8]
# list_y = [1,2,3,4,5,6,7,8]
# r = map(lambda x,y:x*x + y,list_x,list_y)
# print(list(r))  # [2, 6, 12, 20, 30, 42, 56, 72]

list_x = [1,2,3,4,5,6,7,8]
list_y = [1,2,3,4,5,6]
r = map(lambda x,y:x*x + y,list_x,list_y)
print(list(r))    # [2, 6, 12, 20, 30, 42]

#reduce

from functools import reduce

# 连续计算,连续调用lamdba
list_x = [1,2,3,4,5,6,7,8]
# r = reduce(lambda x,y:x+y,list_x)
# print(r)   # 36
# ((((((1+2)+3)+4)+5)+6)+7)+8
# x=1
# y=2

# x=(1+2)
# y=3

# x=((1+2)+3)
# y=4

# x=(((1+2)+3)+4)
# y=5
# ....

# [(1,3),(2,-2),(-2,3),...]

# r = reduce(lambda x,y:x+y,list_x,10)
# print(r)  # 46
# x=1
# y=10

# x=(1+10)
# y=2

# x=((1+10)+2)
# y=3

# x=(((1+10)+2)+3)
# y=4
# ....

# filter

list_x = [1,0,1,0,0,1]
# r = filter(lambda x:True if x==1 else False,list_x)
r = filter(lambda x:x,list_x)
print(list(r)) # [1, 1, 1]

list_u = [‘a‘,‘B‘,‘c‘,‘F‘,‘e‘]

# 装饰器

import time
def f1():
    print(‘this is a function_1‘)

def f2():
    print(‘this is a function_2‘)

def print_current_time(func):
    print(time.time())
    func()

print_current_time(f1)
# 1524300517.6711748
# this is a function_1
print_current_time(f2)
# 1524300517.6711748
# this is a function_2
def decorator(func):
    def wrapper():
        print(time.time())
        func()
    return wrapper

def f1():
    print(‘this is a function_1‘)
f = decorator(f1)
f()
# 1524301122.5020452
# this is a function_1
def decorator(func):
    def wrapper():
        print(time.time())
        func()
    return wrapper

@decorator
def f3():
    print(‘this is a function_2‘)
f3()
# 1524301486.9940615
# this is a function_2

原文地址:https://www.cnblogs.com/zouke1220/p/8902030.html

时间: 2024-07-29 17:49:40

13.函数式编程:匿名函数、高阶函数、装饰器的相关文章

Python学习笔记八:文件操作(续),文件编码与解码,函数,递归,函数式编程介绍,高阶函数

文件操作(续) 获得文件句柄位置,f.tell(),从0开始,按字符数计数 f.read(5),读取5个字符 返回文件句柄到某位置,f.seek(0) 文件在编辑过程中改变编码,f.detech() 获取文件编码,f.encoding() 获取文件在内存中的编号,f.fileno() 获取文件终端类型(tty.打印机等),f.isatty() 获取文件名,f.name() 判断文件句柄是否可移动(tty等不可移动),f.seekable() 判断文件是否可读,f.readable() 判断文件是

C#函数式编程中的标准高阶函数详解

何为高阶函数 大家可能对这个名词并不熟悉,但是这个名词所表达的事物却是我们经常使用到的.只要我们的函数的参数能够接收函数,或者函数能够返回函数,当然动态生成的也包括在内.那么我们就将这类函数叫做高阶函数.但是今天我们的标题并不是高阶函数,而是标准高阶函数,既然加上了这个标准,就意味着在函数式编程中有一套标准的函数,便于我们每次调用.而今天我们将会介绍三个标准函数,分别为Map.Filter.Fold. Map 这个函数的作用就是将列表中的每项从A类型转换到B类型,并形成一个新的类型.下面我们可以

python学习三十四天函数高阶函数定义及用法

python函数高阶函数是把函数当成一个变量,传递给函数作为参数,或者函数的返回值里面有函数,都称为高阶函数, 1,把函数作为参数传递 def dac(x,y): return x+y def test(n): print(n) test(dac) 输出结果为 函数类型function 2,把函数做为返回值,也是高阶函数 def test(x,y): return abs,x,y 输出结果为 列表,包含 函数,参数 文章来自(www.96net.com.cn) 原文地址:https://www.

高阶组件装饰器

高阶组件装饰器  注意利用函数式组件进行化简! import React from 'react'; //1 组件原型 class Reg extends React.Component{ render(){ return <_Reg service={service} />; } } //2 匿名组件 const Reg = class extends React.Component{ render(){ return <_Reg service={service} />; }

002_第三部分_[函数 / 高阶函数 / 装饰器]

一.高阶函数: 顺序排序 enumerate([1,2 ,3 , 4, 5]) for idx, item in enumerate([1, 2, 3, 4]):     print(idex)     print(item) def sort(*args):     ret = []     for item in args:         for i, v in enumerate(ret):             if item > v:                 ret.ins

PYTHON学习0028:函数---高阶函数----2019-6-20

1.定义:变量可以指向函数,函数的参数能够接收变量,那么一个函数就可以接收另一个函数作为参数,这种函数就称之为高阶函数.只要把函数作为参数传到其他函数,不管其他函数有没有返回值,,都叫高阶函数.2.函数也可以接收其他函数作为返回值. 原文地址:https://blog.51cto.com/13543767/2411396

Python 函数/高阶函数

def add(x,y,f): return f(x) + f(y) def make(x): return x + 10 print add(3,-8,make) #输出的结果就是15 编写高阶函数,就是让函数的参数能够接收别的函数. 原文地址:https://www.cnblogs.com/jkklearn/p/11297878.html

高阶函数之函数作为参数

SICP 1.3.1  Procedures as Arguments,说明高阶函数之函数作为参数的原因:若干个函数拥有相似的算法或代码结构,对此加以抽象. (define (sum-integers a b) (if (> a b) 0 (+ a (sum-integers (+ a 1) b)))) (define (pi-sum a b) (if (> a b) 0 (+ (/ 1.0 (* a (+ a 2))) (pi-sum (+ a 4) b)))) 于是: 清单1: (defi

Python进阶:函数式编程(高阶函数,map,reduce,filter,sorted,返回函数,匿名函数,偏函数)...啊啊啊

函数式编程 函数是Python内建支持的一种封装,我们通过把大段代码拆成函数,通过一层一层的函数调用,就可以把复杂任务分解成简单的任务,这种分解可以称之为面向过程的程序设计.函数就是面向过程的程序设计的基本单元. 而函数式编程(请注意多了一个"式"字)--Functional Programming,虽然也可以归结到面向过程的程序设计,但其思想更接近数学计算. 我们首先要搞明白计算机(Computer)和计算(Compute)的概念. 在计算机的层次上,CPU执行的是加减乘除的指令代码