|
Re: 8, you don't need decorators to compose functions: def comp(f, g):
def h(*args, **kwargs):
return g(f(*args, **kwargs))
# fix up h.__doc__ and friends
return h
or simply (lambda the, args: g(f(the, args))(x, y)
(don't remember comp's semantics, is (comp f g) = f o g or g o f?)too long; don't read: Decorators are certainly cool, but semantically they represent something more like a pattern than a FP construct. A decorator represents something you might want to do to lots of functions, a property you want all instances of a function to have without writing it explicitly into each function. Function composition is more along the lines of having two functions which are interesting on their own, but which sometimes you want to compose. With decorators, it would also be awkward to compose multiple functions. Observe: def compose_with(g):
def decorator(f):
def decorated_function(*args, **kwargs):
return g(f(*args, **kwargs))
return decorated_function
return decorator
def h(x): math.sqrt(x)
@compose_with(h)
def g(x): 2 * x
@compose_with(g)
def f(x): x + 1
versus (for some reasonable definition of apply...) def compose(*fns):
def composition(*args, **kwargs):
return reduce((lambda computed, next_fn: next_fn.apply(computed)),
fns,
(args, kwargs))
return composition
# define fns as above without decorator
hogof = compose(f, g, h)
|
(def my-decor (partial comp decor-bevior))
Done.