我正在以函数式风格编写 Python(我认为我所得到的类似于 monad?)。到目前为止,这是我为三个功能硬编码的内容。如果我有 10 个或 100 个呢?
# a list of (function, function, function), each of which accept
# a scalar and return a list
funcs = [(lambda a: [a, a], lambda a: [a, a, a], lambda a: [a])] * 10
possible = []
car = 3
for a, b, c in funcs:
ra = a(car)
if ra:
rb = b(ra[0])
if rb:
rc = c(rb[0])
if rc: # last function
possible.extend(rc)