一、什么是纯函数?
纯函数是函数式编程中的核心概念。一个函数被称为"纯函数",必须满足两个条件:
# 纯函数示例
defadd(a, b):
return a + b
print(add(2, 3)) # 5
print(add(2, 3)) # 5(相同输入,相同输出)
# 非纯函数示例
total = 0
defadd_to_total(x):
global total
total += x # 修改了外部变量(副作用)
return total
二、纯函数的两大特征详解
2.1 确定性(相同输入,相同输出)
纯函数的输出完全由输入参数决定,不受任何外部状态影响。
# ✅ 纯函数
defcircle_area(radius):
return3.14159 * radius * radius
# ❌ 非纯函数(依赖外部变量)
PI = 3.14159
defcircle_area(radius):
return PI * radius * radius # 依赖外部变量
# ❌ 非纯函数(依赖随机数)
import random
defroll_dice():
return random.randint(1, 6) # 每次调用结果不同
2.2 无副作用
纯函数不会:
- • 执行 I/O 操作(打印、读写文件、网络请求)
# ❌ 有副作用的函数
counter = 0
defincrement():
global counter
counter += 1# 修改全局变量
return counter
# ❌ 修改传入的可变对象
defadd_to_list(lst, item):
lst.append(item) # 修改了传入的列表
return lst
# ❌ 有 I/O 操作
deflog_message(msg):
print(msg) # 控制台输出
returnlen(msg)
三、为什么不使用纯函数?
3.1 可测试性
纯函数非常容易测试,因为不需要设置复杂的外部状态。
# ✅ 纯函数:测试简单
defadd(a, b):
return a + b
# 测试只需检查输入输出
assert add(2, 3) == 5
assert add(-1, 1) == 0
# ❌ 非纯函数:测试困难
balance = 100
defwithdraw(amount):
global balance
if amount <= balance:
balance -= amount
returnTrue
returnFalse
# 测试前需要重置 balance 状态
3.2 可缓存性(记忆化)
纯函数的结果可以被缓存,提高性能。
from functools import lru_cache
# 纯函数可以使用 lru_cache
@lru_cache(maxsize=128)
deffibonacci(n):
if n < 2:
return n
return fibonacci(n-1) + fibonacci(n-2)
# 非纯函数不能使用 lru_cache
counter = 0
defimpure_fib(n):
global counter
counter += 1# 副作用,缓存会导致错误
if n < 2:
return n
return impure_fib(n-1) + impure_fib(n-2)
3.3 可并行性
纯函数不会共享状态,可以安全地并发执行。
# 纯函数可以安全地并行执行
defpure_work(x):
return x * x
# 并行处理
from concurrent.futures import ThreadPoolExecutor
with ThreadPoolExecutor() as executor:
results = list(executor.map(pure_work, range(1000)))
3.4 可推理性和可维护性
纯函数的代码更容易理解和调试,因为不需要追踪外部状态变化。
# 纯函数:一目了然
defcalculate_total(price, quantity, tax_rate):
subtotal = price * quantity
tax = subtotal * tax_rate
return subtotal + tax
# 非纯函数:需要理解外部状态
tax_rate = 0.1
defcalculate_total(price, quantity):
global tax_rate
subtotal = price * quantity
# 谁能想到 tax_rate 可能被其他地方修改?
return subtotal + subtotal * tax_rate
三、如何处理副作用
在实际项目中,完全避免副作用是不可能的(程序最终需要 I/O)。函数式编程的建议是:将副作用隔离到程序的边界,核心逻辑保持纯函数。
3.1 分离纯逻辑和非纯操作
import json
# ❌ 不好的设计:纯逻辑和 I/O 混在一起
defprocess_file(filename):
withopen(filename, 'r') as f:
data = json.load(f)
# 业务逻辑
result = {}
for item in data['items']:
result[item['id']] = item['price'] * 1.1
withopen('output.json', 'w') as f:
json.dump(result, f)
# ✅ 好的设计:分离纯逻辑
defprocess_items(items):
"""纯函数:处理数据逻辑"""
result = {}
for item in items:
result[item['id']] = item['price'] * 1.1
return result
defmain():
# I/O 在边界处理
withopen('input.json', 'r') as f:
data = json.load(f)
# 调用纯函数
result = process_items(data['items'])
# I/O 在边界处理
withopen('output.json', 'w') as f:
json.dump(result, f)
3.2 使用不可变数据
# ❌ 修改传入的数据
defadd_discount(prices, discount):
for i inrange(len(prices)):
prices[i] = prices[i] * (1 - discount) # 修改原列表
return prices
# ✅ 返回新数据
defadd_discount(prices, discount):
return [p * (1 - discount) for p in prices] # 返回新列表
prices = [100, 200, 300]
discounted = add_discount(prices, 0.1)
print(prices) # [100, 200, 300](原列表不变)
print(discounted) # [90.0, 180.0, 270.0]
四、纯函数的实际应用
4.1 数据转换管道
# 一组纯函数
deffilter_positive(numbers):
return [n for n in numbers if n > 0]
defsquare(numbers):
return [n ** 2for n in numbers]
defsum_numbers(numbers):
returnsum(numbers)
# 组合成处理管道
numbers = [-3, 1, 2, -5, 3, 4]
result = sum_numbers(square(filter_positive(numbers)))
print(result) # 1^2 + 2^2 + 3^2 + 4^2 = 30
4.2 购物车计算
# 纯函数计算购物车
defcalculate_subtotal(items):
"""计算小计"""
returnsum(item['price'] * item['quantity'] for item in items)
defapply_discount(amount, discount_rate):
"""应用折扣"""
return amount * (1 - discount_rate)
defapply_tax(amount, tax_rate):
"""应用税费"""
return amount * (1 + tax_rate)
defcalculate_total(items, discount_rate=0, tax_rate=0.1):
"""组合纯函数计算总价"""
subtotal = calculate_subtotal(items)
after_discount = apply_discount(subtotal, discount_rate)
total = apply_tax(after_discount, tax_rate)
returnround(total, 2)
# 使用
cart = [
{'name': '苹果', 'price': 8.5, 'quantity': 3},
{'name': '香蕉', 'price': 5.0, 'quantity': 2},
]
total = calculate_total(cart, discount_rate=0.1)
print(f"总价: {total}")
五、高阶函数与纯函数
函数式编程中,map、filter、reduce 等高阶函数通常与纯函数配合使用。
from functools import reduce
# 纯函数:应用于每个元素
defdouble(x):
return x * 2
defis_even(x):
return x % 2 == 0
defadd(x, y):
return x + y
numbers = [1, 2, 3, 4, 5, 6]
# 函数式管道(纯函数组合)
result = reduce(
add,
map(double, filter(is_even, numbers))
)
print(result) # 2+4+6 = 12, double后 4+8+12 = 24
六、总结
核心要点:
- • 纯函数是函数式编程的基石,具有确定性、无副作用的特点。
- • 实际项目中,应将纯逻辑与副作用分离,核心业务逻辑保持纯函数。
- • 不追求"完全纯函数",而是在合适的场景使用纯函数。
掌握纯函数的概念和实践,可以让你的代码更可靠、更易维护,是函数式编程的核心思想之一。