函数式编程
- 定义:用一系列函数解决问题。
– 函数可以赋值给变量,赋值后变量绑定函数。
– 允许将函数作为参数传入另一个函数。
– 允许函数返回一个函数。- 高阶函数:将函数作为参数或返回值的函数。
函数作为参数
将核心逻辑传入方法体,使该方法的适用性更广,体现了面向对象的开闭原则。
lambda 表达式 - 定义:是一种匿名方法。
- 作用:作为参数传递时语法简洁,优雅,代码可读性强。
随时创建和销毁,减少程序耦合度。 - 语法
– 定义:
变量 = lambda 形参: 方法体
– 调用:
变量(实参) - 说明:
– 形参没有可以不填
– 方法体只能有一条语句,且不支持赋值语句。
内置高阶函数
- 高阶函数:将函数作为参数或返回值的函数。
- map(函数,可迭代对象):使用可迭代对象中的每个元素调用函数,将返回值作为新可迭代对象元素;返回值为新可迭代对象。
2. filter(函数,可迭代对象):根据条件筛选可迭代对象中的元素,返回值为新可迭代对象。
3. sorted(可迭代对象,key = 函数,reverse = bool值):排序,返回值为排序结果。
4. max(可迭代对象,key = 函数):根据函数获取可迭代对象的最大值。
5. min(可迭代对象,key = 函数):根据函数获取可迭代对象的最小值。
"""
需求:
1. 定义函数,在老婆列表中查找所有老婆的姓名
2. 定义函数,在老婆列表中查找所有老婆的姓名和身高
需求:
1. 定义函数,在老婆列表中累加所有老婆的总身高
2. 定义函数,在老婆列表中累加所有老婆的总体重
需求:
1. 定义函数,在老婆列表中删除年龄在20--25之间的所有老婆
2. 定义函数,在老婆列表中删除体重大于160的所有老婆
需求:
1. 定义函数,在老婆列表中找出年龄最大的老婆
2. 定义函数,在老婆列表中找出身高最高的老婆
需求:
1. 定义函数,根据体重对老婆列表进行升序排列
2. 定义函数,根据身高对老婆列表进行升序排列
步骤:
1. 将需求完整实现到函数中。
2. 将变化点单独定义到函数中。
3. 将通用代码定义到函数中。
4. 用参数隔离变化点。
5. 将通用代码移动到IterableHelper类中
6. 测试调用IterableHelper的静态方法执行功能。
实际开发:
5、6
"""
from common.iterable_tools import IterableHelper
class Wife:
"""
抽象的数据
"""
def __init__(self, name="", age=0, height=0, weight=0):
self.name = name
self.age = age
self.height = height
self.weight = weight
def __str__(self):
return "%s--%d--%d--%d" % (self.name, self.age, self.height, self.weight)
list01 = [
Wife("铁锤", 27, 190, 200),
Wife("铁钉", 37, 165, 160),
Wife("铁棒", 24, 160, 190),
Wife("铁锅", 23, 190, 100),
]
def condition01(item):
return item.age > 25
def condition02(item):
return item.height < 180
for item in IterableHelper.find_all(list01, condition01):
print(item)
# ---------------------练习1-----------------------------
"""
def select01():
for item in list01:
yield item.name
def select02():
for item in list01:
yield (item.name,item.height)
"""
def handle01(item):
return item.name
def handle02(item):
return (item.name, item.height)
handle01(Wife("铁锤", 27, 190, 200))
"""
def select(func):
for item in list01:
# yield (item.name,item.height)
# yield handle02(item)
yield func(item)
"""
for item in IterableHelper.select(list01, handle01):
print(item)
for item in IterableHelper.select(list01, lambda item: item.name):
print(item)
# ---------------------练习2-----------------------------
# 1.定义函数,在老婆列表中累加所有老婆的总身高
"""
def sum01():
sum_value = 0
for item in list01:
sum_value += item.height
return sum_value
def sum02():
sum_value = 0
for item in list01:
sum_value += item.weight
return sum_value
def handle01(item):
return item.height
def handle02(item):
return item.weight
def sum(func):
sum_value = 0
for item in list01:
# sum_value += item.weight
# sum_value += handle02(item)
sum_value += func(item)
return sum_value
"""
re = IterableHelper.sum(list01, lambda item: item.height)
print(re)
# ---------------------练习3-----------------------------
# re = IterableHelper.delete_all(list01,lambda item:20 <= item.age <= 25)
# print(re)
# re = IterableHelper.delete_all(list01,lambda element:element.weight > 160)
# print(re)
# ---------------------练习4-----------------------------
re = IterableHelper.get_max(list01,lambda e:e.age)
# print(re)
re = IterableHelper.get_max(list01,lambda e:e.height)
# print(re)
# ---------------------练习5-----------------------------
# IterableHelper.order_by(list01,lambda item:item.age)
IterableHelper.order_by(list01,lambda item:item.height)
for item in list01:
print(item)
集成操作框架如下:
class IterableHelper:
"""
可迭代对象助手
微软 集成操作框架
"""
# 静态方法 -- 不会自动传递参数
@staticmethod
def find_all(iterable, func_condition):
"""
通用的查找满足条件的所有元素。
:param iterable:需要被搜索的可迭代对象
:param func_condition:搜索的条件
:return:生成器类型,可以推算出满足条件的元素
"""
for item in iterable:
if func_condition(item):
yield item
@staticmethod
def select(iterable, func_handle):
"""
通用的筛选可迭代对象中的元素。
:param iterable:需要被筛选的可迭代对象
:param func_handle:筛选的处理逻辑
:return:生成器类型,可以推算出筛选的元素
"""
for item in iterable:
yield func_handle(item)
@staticmethod
def sum(iterable, func_handle):
"""
通用的累加功能
:param iterable: 需要累加的可迭代对象
:param func_handle: 累加的逻辑
:return:累加结果
"""
sum_value = 0
for item in iterable:
sum_value += func_handle(item)
return sum_value
@staticmethod
def delete_all(iterable, func_condition):
count = 0
for i in range(len(iterable) - 1, -1, -1):
# if 20 <= iterable[i].age <= 25:
if func_condition(iterable[i]):
del iterable[i]
count += 1
return count
# lambda e:e.age
@staticmethod
def get_max(iterable, func_condition):
max_value = iterable[0]
for i in range(1, len(iterable)):
# if max_value.age < iterable[i].age:
if func_condition(max_value) < func_condition(iterable[i]):
max_value = iterable[i]
return max_value
@staticmethod
def order_by(iterable,func_condition):
for r in range(len(iterable) - 1):
for c in range(r + 1, len(iterable)):
# if iterable[r].height > iterable[c].height:
if func_condition(iterable[r]) > func_condition(iterable[c]):
iterable[r], iterable[c] = iterable[c], iterable[r]