[Python-ideas] Syntax for curried functions

Bruce Frederiksen dangyogi at gmail.com
Mon Mar 2 15:45:56 CET 2009


Weeble wrote:
> Would this be crazy? Make this:
>
> def spam(a,b,c)(d,e,f):
>     [BODY]
>
> a synonym for:
>
> def spam(a,b,c):
>     def spam2(d,e,f):
>         [BODY]
>     return spam2
>   
Why not:

def curry(n):
    def decorator(fn):
        @functools.wraps(fn)
        def surrogate(*args):
            if len(args) != n:
                raise TypeError("%s() takes exactly %d arguments (%d 
given)" %
                                (fn.__name__, n, len(args)))
            def curried(*rest, **kws):
                return fn(*(args + rest), **kws)
            return curried
        return surrogate
    return decorator

@curry(3)
def spam(a,b,c,d,e,f):
    print a,b,c,d,e,f

spam(1,2,3)(4,5,6)


-bruce frederiksen



More information about the Python-ideas mailing list