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.upd ate(fkeywords)
newargs = (lambda seq: tuple([(a == _ and a(seq)) or a for
a in args] + seq))(list(farg s))
return func(*newargs, **newkeywords)
newfunc.func = func
newfunc.args = args
newfunc.keyword s = keywords
return newfunc
Here is example of use:
[color=blue][color=green][color=darkred]
>>> def capture(*args):[/color][/color][/color]
return args
[color=blue][color=green][color=darkred]
>>> partial(capture )()[/color][/color][/color]
()[color=blue][color=green][color=darkred]
>>> partial(capture , _)(1)[/color][/color][/color]
(1,)[color=blue][color=green][color=darkred]
>>> partial(capture , _, 2)(1)[/color][/color][/color]
(1, 2)[color=blue][color=green][color=darkred]
>>> partial(capture , 1)(2)[/color][/color][/color]
(1, 2)[color=blue][color=green][color=darkred]
>>> partial(capture , 1, _)(2)[/color][/color][/color]
(1, 2)[color=blue][color=green][color=darkred]
>>> partial(capture , 1, _)()[/color][/color][/color]
IndexError: pop from empty list[color=blue][color=green][color=darkred]
>>> partial(capture , 1, _, _)(2, 3)[/color][/color][/color]
(1, 2, 3)
Chris Perkins :
[color=blue]
> 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(curr y(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[/color]
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.upd ate(fkeywords)
newargs = (lambda seq: tuple([(a == _ and a(seq)) or a for
a in args] + seq))(list(farg s))
return func(*newargs, **newkeywords)
newfunc.func = func
newfunc.args = args
newfunc.keyword s = keywords
return newfunc
Here is example of use:
[color=blue][color=green][color=darkred]
>>> def capture(*args):[/color][/color][/color]
return args
[color=blue][color=green][color=darkred]
>>> partial(capture )()[/color][/color][/color]
()[color=blue][color=green][color=darkred]
>>> partial(capture , _)(1)[/color][/color][/color]
(1,)[color=blue][color=green][color=darkred]
>>> partial(capture , _, 2)(1)[/color][/color][/color]
(1, 2)[color=blue][color=green][color=darkred]
>>> partial(capture , 1)(2)[/color][/color][/color]
(1, 2)[color=blue][color=green][color=darkred]
>>> partial(capture , 1, _)(2)[/color][/color][/color]
(1, 2)[color=blue][color=green][color=darkred]
>>> partial(capture , 1, _)()[/color][/color][/color]
IndexError: pop from empty list[color=blue][color=green][color=darkred]
>>> partial(capture , 1, _, _)(2, 3)[/color][/color][/color]
(1, 2, 3)
Chris Perkins :
[color=blue]
> 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(curr y(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[/color]
Comment