Open eriknw opened 10 years ago
It could be convenient to have a function that turns a first order function into a higher-order function (i.e. a "decorator" decorator), and vice versa. Crude examples are below:
def identity(x): return x def higher(f): def outer(g): def inner(*args, **kwargs): return f(g(*args, **kwargs)) return inner return outer def lower(f): return f(identity) # Test functions def double(x): return 2 * x def doubled(f): def inner(*args, **kwargs): return 2 * f(*args, **kwargs) return inner highdouble = higher(double) lowdouble1 = lower(highdouble) lowdouble2 = lower(doubled) assert doubled(lambda: 10)() == 20 assert highdouble(lambda: 10)() == 20 assert double(10) == 20 assert lowdouble1(10) == 20 assert lowdouble2(10) == 20
Heh, functionalize and defunctionalize.
functionalize
defunctionalize
It could be convenient to have a function that turns a first order function into a higher-order function (i.e. a "decorator" decorator), and vice versa. Crude examples are below: