– Start
点击此处观看本系列配套视频。
什么是函数式编程?
其实函数式编程和 SQL 语句很像,它们操作的对象是一个集合。最开始,几乎所有的语言都是面向过程的,而 SQL 是面向集合的,后来由于业务需求越来越复杂,各大数据库厂商推出了面向过程的 SQL,如 PL/SQL。同时各种编程语言也开始支持面向集合的编程方式,即函数式编程。
过程式编程 vs 函数式编程
下面的例子演示过程式和函数式编程来求最大值,可以看到函数式编程只要一行代码就实现了功能。
my_list = [1, 5, 3, 8]
# 过程式编程
max_number = 0
for n in my_list:
if n > max_number:
max_number = n
print(max_number)
# 函数式编程
max_number = max(my_list)
print(max_number)
函数式编程 vs SQL
例子说明
假设我们有个订单表 Order,有如下几列。
order_id, order_datetime, buyer, seller, goods, status, amount
假设我们有下面的订单。
# order_id, order_datetime, buyer, seller, goods, status, amount
class Order:
def __init__(self, order_id, order_datetime, buyer, seller, goods, status, amount):
self.order_id = order_id
self.order_datetime = order_datetime
self.buyer = buyer
self.seller = seller
self.goods = goods
self.status = status
self.amount = amount
def __str__(self):
return f'(order_id={self.order_id}, order_datetime={self.order_datetime}, buyer={self.buyer}, ' \
f'seller={self.seller}, goods={self.goods}, status={self.status}, amount={self.amount})'
def __repr__(self):
return self.__str__()
o1 = Order(1, datetime(2018, 9, 30, 1), '张三', '小米', '电话', '已发货', 1999)
o2 = Order(2, datetime(2018, 9, 30, 2), '李四', '小米', '电话', '已发货', 1999)
o3 = Order(3, datetime(2018, 9, 30, 3), '张三', '联想', '电脑', '完成', 6999)
o4 = Order(4, datetime(2018, 9, 30, 4), '李四', '小米', '电脑', '完成', 4999)
order_list = [o1, o2, o3, o4]
filter VS where
# select * from Order where buyer = '张三'
print(list(
filter(lambda order: order.buyer == '张三', order_list)
))
# select * from Order where buyer = '张三' and status = '已发货'
print(list(
filter(lambda order: order.buyer == '张三' and order.status == '已发货', order_list)
))
sorted VS order by
# select * from Order where buyer = '张三' order by order_datetime
print(list(
sorted(
filter(lambda order: order.buyer == '张三', order_list),
key=lambda order: order.order_datetime
)
))
# select * from Order where buyer = '张三' order by order_datetime desc
print(list(
sorted(
filter(lambda order: order.buyer == '张三', order_list),
key=lambda order: order.order_datetime,
reverse=True
)
))
map VS select xx from
# select goods from Order where buyer = '张三'
print(list(
map(
lambda order: order.goods,
filter(lambda order: order.buyer == '张三', order_list)
)
))
# select goods, seller from Order where buyer = '张三'
print(list(
map(
lambda order: (order.goods, order.seller),
filter(lambda order: order.buyer == '张三', order_list)
)
))
set VS distinct
# select distinct goods from Order where buyer = '张三'
print(set(
map(
lambda order: order.goods,
filter(lambda order: order.buyer == '张三', order_list)
)
))
min, max, sum, count, avg
# select min(amount) from Order where buyer = '张三'
print(min(list(
map(
lambda order: order.amount,
filter(lambda order: order.buyer == '张三', order_list)
)
)))
# select max(amount) from Order where buyer = '张三'
print(max(list(
map(
lambda order: order.amount,
filter(lambda order: order.buyer == '张三', order_list)
)
)))
# select sum(amount) from Order where buyer = '张三'
print(sum(
map(
lambda order: order.amount,
filter(lambda order: order.buyer == '张三', order_list)
)
))
# select count(*) from Order where buyer = '张三'
print(len(list(
filter(lambda order: order.buyer == '张三', order_list)
)))
# select avg(*) from Order where buyer = '张三'
r = list(
map(
lambda order: order.amount,
filter(lambda order: order.buyer == '张三', order_list)
)
)
if r:
print(sum(r) / len(r))
itertools.groupby VS group by
# select buyer, min(amount) from Order group by buyer
print([(b, min(map(lambda order: order.amount, g))) for b, g in it.groupby(sorted(order_list, key=lambda order: order.buyer), key=lambda order: order.buyer)])
# select buyer, max(amount) from Order group by buyer
print([(b, max(map(lambda order: order.amount, g))) for b, g in it.groupby(sorted(order_list, key=lambda order: order.buyer), key=lambda order: order.buyer)])
# select buyer, sum(amount) from Order group by buyer
print([(b, sum(map(lambda order: order.amount, g))) for b, g in it.groupby(sorted(order_list, key=lambda order: order.buyer), key=lambda order: order.buyer)])
# select buyer, count(*) from Order group by buyer
print([(b, len(list(g))) for b, g in it.groupby(sorted(order_list, key=lambda order: order.buyer), key=lambda order: order.buyer)])
itertools.count VS rownum
# select rownum, buyer, goods from Order
rownum = it.count(1)
print(list(map(lambda order: (next(rownum), order.buyer, order.goods), order_list)))
– 更多参见:Python 精萃
– 声 明:转载请注明出处
– Last Updated on 2018-10-02
– Written by ShangBo on 2018-10-02
– End