D41d8cd98f00b204e9800998ecf8427e

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.

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]), [])
@curried
def add(a,b):
    return a + b
map(add(10), range(3)) # == [10,11,12]

Refactorings

No refactoring yet !

673a112cceaf8a4714ac6c3b6ae91690

brent

March 5, 2008, March 05, 2008 18:31, permalink

No rating. Login to rate!
def add(a, b):
    return a + b

add10 = add.__get__(10)

print map(add10, range(3))

Your refactoring





Format Copy from initial code

or Cancel