1 2 3 4 5 6 7 8
import inspect def curried(f): def curhelp(n, args): if n==0: return f(*args) else: return lambda arg: curhelp(n-1, args+[arg]) return curhelp(len(inspect.getargspec(f)[0]), [])
example use ##
1 2 3 4
@curried def add(a,b): return a + b map(add(10), range(3)) # == [10,11,12]
Refactorings
No refactoring yet !
brent
March 5, 2008, March 05, 2008 18:31, permalink
1 2 3 4 5 6
def add(a, b): return a + b add10 = add.__get__(10) print map(add10, range(3))
This decorator works, but uses recursion and re-allocates the args list after each application of the curried function. Can you get rid of one or both?
I googled a bit, but all the example code I found is for partial application instead of true currying.