Partial function application
Dear developers, First of all, I'm a programmer for a data science company and I recently graduated. That being said, I have wondered why python does not have syntactical support (like syntax sugar) for partial function application. I think partial function application is a powerful concept, but also think that the implementation in functional.partial as described here: https://www.python.org/dev/peps/pep-0309/ is too verbose. Moreover I think the functional programming paradigm is a powerful one in general, especially when implemented as much as possible in an iterative language as python avoiding the typical problems we have with purely functional languages like Haskell. An plea for this concept is that, for example, small pieces Haskell (or functional) code can be extremely expressive and concise. I was wondering if there are considerations for including partial function application syntactically. I very often find myself writing statements as: data_sorted = sort(data, key = lambda x: x[0]) where I would prefer data_sorted = sort(data, key = #1[0]) where the #1 is similar to the one used in Mathematica for the same purpose. That is, an expression with #1 becomes an anonymous function which takes one argument, and, obviously, if a #n is included, the anonymous function takes n arguments. Notice that #1[0] does not seem like partial function application, however it is if you (C++'isly) assume that the deference operation [] is a function dereference(subscriptable, subscript). Obviously, I am only expecting that you get these type of suggestions all the time and that there is a good change you do not find it adequate for many reasons, for example keeping the language compact. I this case, I would also like to hear so. Yours sincerely and thank you in advance, Herbert
On Fri, 11 Sep 2015 at 10:41 Herbert Kruitbosch < python-dev@herbertkruitbosch.com> wrote:
Dear developers,
First of all, I'm a programmer for a data science company and I recently graduated.
That being said, I have wondered why python does not have syntactical support (like syntax sugar) for partial function application. I think partial function application is a powerful concept, but also think that the implementation in functional.partial as described here:
https://www.python.org/dev/peps/pep-0309/
is too verbose. Moreover I think the functional programming paradigm is a powerful one in general, especially when implemented as much as possible in an iterative language as python avoiding the typical problems we have with purely functional languages like Haskell. An plea for this concept is that, for example, small pieces Haskell (or functional) code can be extremely expressive and concise.
I was wondering if there are considerations for including partial function application syntactically. I very often find myself writing statements as:
data_sorted = sort(data, key = lambda x: x[0])
To start off, I wouldn't write it that way, but this way: data_sorted - sort(data, key=operator.itemgetter(0))
where I would prefer
data_sorted = sort(data, key = #1[0])
That syntax won't work because `#` is used to start a comment and there is no way to disambiguate that in the grammar.
where the #1 is similar to the one used in Mathematica for the same purpose. That is, an expression with #1 becomes an anonymous function which takes one argument, and, obviously, if a #n is included, the anonymous function takes n arguments.
Notice that #1[0] does not seem like partial function application, however it is if you (C++'isly) assume that the deference operation [] is a function dereference(subscriptable, subscript).
Obviously, I am only expecting that you get these type of suggestions all the time and that there is a good change you do not find it adequate for many reasons, for example keeping the language compact. I this case, I would also like to hear so.
In my code the few times I want partial function applications I have found the functions in the operator module meet that need, else functools.partial or a quickly written closure do the trick. I don't think dedicated syntax is warranted when those other options already exist to meet the same need.
Yours sincerely and thank you in advance,
Thanks for the suggestion! -Brett
Herbert
_______________________________________________ Python-Dev mailing list Python-Dev@python.org https://mail.python.org/mailman/listinfo/python-dev Unsubscribe: https://mail.python.org/mailman/options/python-dev/brett%40python.org
On Fri, 11 Sep 2015 at 10:41 Herbert Kruitbosch <python-dev@herbertkruitbosch.com> wrote: __
data_sorted = sort(data, key = #1[0])
That syntax won't work because `#` is used to start a comment and there is no way to disambiguate that in the grammar.
Obviously :) The #-syntax is used by Mathematica, which is why I used it here. As you stated, it is not adequate for python3. You could imagine a $n or ***n syntax. I'm not in any way well informed on the python3 syntax, nor what would be aesthetically appealing and readable.
where the #1 is similar to the one used in Mathematica for the same purpose. That is, an expression with #1 becomes an anonymous function which takes one argument, and, obviously, if a #n is included, the anonymous function takes n arguments.
Notice that #1[0] does not seem like partial function application, however it is if you (C++'isly) assume that the deference operation [] is a function dereference(subscriptable, subscript).
Obviously, I am only expecting that you get these type of suggestions all the time and that there is a good change you do not find it adequate for many reasons, for example keeping the language compact. I this case, I would also like to hear so.
In my code the few times I want partial function applications I have found the functions in the operator module meet that need, else functools.partial or a quickly written closure do the trick. I don't think dedicated syntax is warranted when those other options already exist to meet the same need.
I would guess it depends on personal preference. And like you stated, there are many clear and intuitive workarounds. I never got to the point where functools.partial and operator.* became part of my coding equipment, and I would adore this feature. I'm very curious if there are more people on my team, but am also interested to hear about opinions against such a feature.
Thanks for the suggestion!
No problem, and thank you for reading it :) Herbert
On 9/11/2015 3:56 AM, Herbert Kruitbosch wrote:
I was wondering if there are considerations for including partial function application syntactically. I very often find myself writing statements as: data_sorted = sort(data, key = lambda x: x[0]) where I would prefer data_sorted = sort(data, key = #1[0]) where the #1 is similar to the one used in Mathematica for the same purpose. That is, an expression with #1 becomes an anonymous function which takes one argument, and, obviously, if a #n is included, the anonymous function takes n arguments.
For future reference, ideas like this belong on the python-list or python-ideas list. Pydev is more for discussion of policy, concrete issues for the next releases, and approval ideas that have been fully fleshed out in the form of a PEP. -- Terry Jan Reedy
participants (3)
-
Brett Cannon -
Herbert Kruitbosch -
Terry Reedy