PEP 309 (Partial Function Application) Idea

Ronald Mai RonaldMatt at gmail.com
Sun Jan 15 01:52:38 EST 2006


In my opinion, Ellipsis might be in the middle, not only in leftmost or
rightmost, so a placeholder approach can be much more flexible and
convenient.

Here is a reference implementation:

_ = lambda x: x.pop(0)

def partial(func, *args, **keywords):
        def newfunc(*fargs, **fkeywords):
            newkeywords = keywords.copy()
            newkeywords.update(fkeywords)
            newargs = (lambda seq: tuple([(a == _ and a(seq)) or a for
a in args] + seq))(list(fargs))
            return func(*newargs, **newkeywords)
        newfunc.func = func
        newfunc.args = args
        newfunc.keywords = keywords
        return newfunc

Here is example of use:

>>> def capture(*args):
	return args

>>> partial(capture)()
()
>>> partial(capture, _)(1)
(1,)
>>> partial(capture, _, 2)(1)
(1, 2)
>>> partial(capture, 1)(2)
(1, 2)
>>> partial(capture, 1, _)(2)
(1, 2)
>>> partial(capture, 1, _)()
IndexError: pop from empty list
>>> partial(capture, 1, _, _)(2, 3)
(1, 2, 3)

Chris Perkins :

> Random idea of the day: How about having syntax support for
> currying/partial function application, like this:
>
> func(..., a, b)
> func(a, ..., b)
> func(a, b, ...)
>
> That is:
> 1) Make an Ellipsis literal legal syntax in an argument list.
> 2) Have the compiler recognize the Ellipsis literal and transform the
> function call into a curried/parially applied function.
>
> So the compiler would essentially do something like this:
>
> func(a, ...) ==> curry(func, a)
> func(..., a) ==> rightcurry(func, a)
> func(a, ..., b) ==> rightcurry(curry(func,a), b)
>
> I haven't though this through much, and I'm sure there are issues, but
> I do like the way it looks.  The "..." really stands out as saying
> "something is omitted here".
> 
> 
> Chris Perkins




More information about the Python-list mailing list