PEP 309 (Partial Function Application) Idea

R

Ronald Mai

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:
return args
(1, 2, 3)

Chris Perkins :
 
G

Giovanni Bajo

Ronald said:
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:
return args
(1, 2, 3)

Other implementations I have seen (boost::bind comes to mind) use ordered
placeholders such as _1, _2, _3, etc to provide more flexibility in adaptation:
("a", "c", "b")

I don't see mention of this in the PEP, but it's a nice feature to have IMO.
 
B

bonono

Giovanni said:
Ronald said:
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)

Other implementations I have seen (boost::bind comes to mind) use ordered
placeholders such as _1, _2, _3, etc to provide more flexibility in adaptation:
("a", "c", "b")

I don't see mention of this in the PEP, but it's a nice feature to have IMO.
Since python has named parameter(and I assume this PEP would support it
as well), is it really that useful to have these place holder things ?
As when the parameter list gets long, named param should be easier to
read.

The only case I find it useful is for binary ops where I would like to
either bind the left hand side or the right hand side but that can be
handled easily with a "flip" function as in haskell.
 
G

Giovanni Bajo

Since python has named parameter(and I assume this PEP would support
it as well), is it really that useful to have these place holder things
?

Probably not so much, you're right.
As when the parameter list gets long, named param should be easier to
read.

The only case I find it useful is for binary ops where I would like to
either bind the left hand side or the right hand side but that can be
handled easily with a "flip" function as in haskell.

I'm not sure I'd like a specialized flip function, compared to the numbered
placeholders solution.
 
R

Ronald Mai

for a new function, named parameter might be a better choise, but for
existing function, placeholder would be better, since you don't need to
modify the exiting code.
 
R

Ronald Mai

for a new function, named parameter might be a better choise, but for
existing function, placeholder would be better, since you don't need to
modify the existing code.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,539
Members
45,024
Latest member
ARDU_PROgrammER

Latest Threads

Top