#Python #code 突然想到
还有 product ,真有意思, equivalent to nested loop:
tee 用于复制流,也有一定组合潜力
还有一个版本 #trap
会 yield 个 stop ,原来是在 while != 检查之前 yield acc 了... 变量更新表还是要跟踪下 😴
from itertools import accumulate话说 permutations 是指 combinations 的 (a,b) 形式啊... 这么高级
def generate(op, init, stop):
acc = init
while acc != stop: yield acc; acc = op(acc)
list(accumulate("是这样呢", lambda s,x: s+x))
_ == ['是', '是这', '是这样', '是这样呢']
list(generate(lambda s: s[1:], "不可能啊", ""))
_ == ['不可能啊', '可能啊', '能啊', '啊']
还有 product ,真有意思, equivalent to nested loop:
product(xs, ys) = ((x,y) for x in xs for y in ys)
tee 用于复制流,也有一定组合潜力
还有一个版本 #trap
def generate(op, init, stop):
acc = init; yield acc
while acc != stop: acc = op(acc); yield acc
会 yield 个 stop ,原来是在 while != 检查之前 yield acc 了... 变量更新表还是要跟踪下 😴