Syntax for curried functions

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 As far as I can tell, it would always be illegal syntax right now, so it seems a safe change. And it would make it cleaner to write higher order functions. While I had the idea bouncing round my head, I realised I would quite like the syntax for writing methods too. E.g.: class Eggs(object): def spam(self)(a,b,c): [BODY] Of course, that wouldn't work unless you wrote and used a special meta-class to change how bound methods work (instead of creating a bound method with the instance and function, you'd call the "method function" with the instance and get back the "bound method"), and I have no idea, but I guess it might slow things down. But I thought it was worth mentioning because it looked cool. Anyway, that's tangential to the idea. The idea is that any (positive) number of parameter lists could follow the method name in its declaration. The function takes as input its first parameter list and returns a function that takes the second parameter list, etc., until there are no more method lists and the last level of function executes and returns the result of the function body.

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
As far as I can tell, it would always be illegal syntax right now, so it seems a safe change. And it would make it cleaner to write higher order functions.
I don't think it would. Your proposed syntax is only useful in the case that the outer function has no doc string and there's no pre-processing or post-processing of the inner function, including decorators. In practice, when I write higher order functions, I often do something like this example: # untested def spam_factory(n): """Factory returning decorators that print 'spam' n times.""" msg = "spam " * n if n > 5: msg += 'WONDERFUL SPAM!!!' def decorator(func): """Print 'spam' %d times.""" @functools.wraps(func) def f(*args, *kwargs): print msg return func(*args, **kwargs) return f decorator.__doc__ = decorator.__doc__ % n return decorator Although the function as given is contrived, the basic structure (including doc strings, decorators, pre-processing and post-processing) is realistic, and your suggested syntax wouldn't be useful here. -- Steven

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

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
What advantage does this style have over: def spam(a, b, c, d, e, f): [BODY] spam3 = functools.partial(spam, 'initial', 'three', 'args') --Scott David Daniels Scott.Daniels@Acm.Org

On 2 Mar 2009, at 04:22, 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
I once proposed (as far as I can tell) the exact same thing. Here's the discussion that took place -- http://markmail.org/message/aa22tnx2vog3rnin I still like the idea, but it doesn't appear to be very popular.

On Mar 2, 11:05 pm, Adam Atlas <a...@atlas.st> wrote:
I once proposed (as far as I can tell) the exact same thing. Here's the discussion that took place --http://markmail.org/message/aa22tnx2vog3rnin
I still like the idea, but it doesn't appear to be very popular.
Thank you, at least I feel a little less foolish now. I did try to search for such a proposal, but it's hard to know what search terms to use. To be honest I don't think it's generally useful enough to merit a change to the language, but somehow the idea floated into my head and it just seemed so *neat* that I had to tell somebody. It may be that it just appeals to my sense of aesthetics.

Weeble wrote:
I don't think it's generally useful enough to merit a change to the language, but somehow the idea floated into my head and it just seemed so *neat* that I had to tell somebody.
The designers of at least one other language seem to think it's neat, too. Scheme has an exactly analogous construct: (define ((f x) y) ...) which is shorthand for (define f (lambda (x) (lambda (y) ...))) [The *really* neat thing about the Scheme version is the way it just naturally falls out of the macro expansion of 'define' in terms of 'lambda' -- so it's not really a separate language feature at all!] -- Greg

Haskell has this too, perhaps even more extreme: there's not really such a thing in Haskell as a function of N arguments (N > 1). "f a b = ..." defines a function f of one argument a which returns another function ("f a") of one argument b. And so on. That doesn't mean we need to copy this idea in Python. On Tue, Mar 3, 2009 at 12:56 PM, Greg Ewing <greg.ewing@canterbury.ac.nz> wrote:
Weeble wrote:
I don't think it's generally useful enough to merit a change to the language, but somehow the idea floated into my head and it just seemed so *neat* that I had to tell somebody.
The designers of at least one other language seem to think it's neat, too. Scheme has an exactly analogous construct:
(define ((f x) y) ...)
which is shorthand for
(define f (lambda (x) (lambda (y) ...)))
[The *really* neat thing about the Scheme version is the way it just naturally falls out of the macro expansion of 'define' in terms of 'lambda' -- so it's not really a separate language feature at all!]
-- Greg _______________________________________________ Python-ideas mailing list Python-ideas@python.org http://mail.python.org/mailman/listinfo/python-ideas
-- --Guido van Rossum (home page: http://www.python.org/~guido/)

On Mon, Mar 02, 2009, Weeble wrote:
On Mar 2, 11:05?pm, Adam Atlas <a...@atlas.st> wrote:
I once proposed (as far as I can tell) the exact same thing. Here's ? the discussion that took place --http://markmail.org/message/aa22tnx2vog3rnin
I still like the idea, but it doesn't appear to be very popular.
Thank you, at least I feel a little less foolish now. I did try to search for such a proposal, but it's hard to know what search terms to use. To be honest I don't think it's generally useful enough to merit a change to the language, but somehow the idea floated into my head and it just seemed so *neat* that I had to tell somebody. It may be that it just appeals to my sense of aesthetics.
Don't worry too much about it -- part of the point of python-ideas is to provide an appropriate forum for trial ballons like this. It's only foolish when you haven't done your homework, especially for issues that have previously been brought up ad nauseum. It's also a good idea to keep in mind that even when people response with "ewww, yuck!" they're attacking your idea, not you (yes, I know how difficult that can be ;-). -- Aahz (aahz@pythoncraft.com) <*> http://www.pythoncraft.com/ Weinberg's Second Law: If builders built buildings the way programmers wrote programs, then the first woodpecker that came along would destroy civilization.

On Mon, Mar 2, 2009 at 7:22 PM, Weeble <clockworksaint@gmail.com> wrote:
Thank you, at least I feel a little less foolish now. I did try to search for such a proposal, but it's hard to know what search terms to use. To be honest I don't think it's generally useful enough to merit a change to the language, but somehow the idea floated into my head and it just seemed so *neat* that I had to tell somebody. It may be that it just appeals to my sense of aesthetics.
If it makes you feel better, you can still get almost all of the way there with decorators, and it's even "officially" documented: http://wiki.python.org/moin/PythonDecoratorLibrary#Pseudo-currying -- Cheers, Leif
participants (10)
-
Aahz
-
Adam Atlas
-
Bruce Frederiksen
-
Greg Ewing
-
Guido van Rossum
-
Leif Walsh
-
Scott David Daniels
-
Steven D'Aprano
-
Sturla Molden
-
Weeble