而且总感觉里面还能挖出点什么。总是想要变换一下视角,看看能变出什么。
变换表象,挣扎图存。这是我的一个联想。
#f: state --> state
#where state is an integer
#finite states
#there is a special state 0 stands for error state
sequential and parallel
代码: 全选
from functools import reduce
#sequential - or composition
def seq(*fs):
def s2(f, g):
def fn(s):
return g(f(s))
return fn
return reduce(s2, fs)
#parallel
def par(*fs):
def p2(f, g):
def fn(s):
return f(s) or g(s)
return fn
return reduce(p2, fs)
? - optional
* - zero or more
+ - one or more
代码: 全选
#identity - or blank
def id(s):
return s
#optional
def opt(f):
return par(f, id)
#one or more
def mor(f):
return seq(f, zmo(f))
#zero or more
def zmo(f):
#basically, return opt(mor(f))
#but, make sure it's zero-safe
return opt(lambda s: mor(f)(s) if s else s)