What's wrong with using @? If I understand correctly, it's used for matrix multiplication, which is far enough from function composition to avoid confusion. And it's slightly similar visually to a circle.
On Sunday, May 24, 2020, at 08:07 -0400, Steven D'Aprano wrote:
> On Sun, May 24, 2020 at 02:27:00PM +0300, Ram Rachum wrote:
>
>> Today I wrote a script and did this:
>>
>> sorted(paths, key=lambda path: len(str(path)), reverse=True)
>>
>> But it would have been nicer if I could do this:
>>
>> sorted(paths, key=len, reverse=True)
>
> It would have been even nicer if we could compose functions:
>
> sorted(paths, key=len∘str, reverse=True)
>
> *semi-wink*
It started with y = len(str(f(g(h(x))))), which is ugly. Some people
like pipes, and wrote object-like functions that could be composed with
the "|" character slash:
y = x | h | g | f | str | len
(Or something like that. Maybe there was a ">" at the beginning.) Then
a clojure fan I know showed me function called "thread":
f = thread([len, str, f, g, h])
y = f(x)
An untested Python implementation:
def thread(fs): # or maybe you like def thread(*fs) instead
fs = reversed(fs) # or not, depending on how fs was constructed
def inner(x):
for f in fs:
x = f(x)
return x
return inner
sorted(paths, key=thread([len, str]), reverse=True)
On the one hand, it's not quite as concise as composing the functions
directly. On the other hand, ∘ ruffles a lot of ASCII feathers (but I'm
sure Steven knows that).
_______________________________________________
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-leave@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at https://mail.python.org/archives/list/python-ideas@python.org/message/6WEANUFPLQY4F6IEK5MO6VYOZ6M7SJ22/
Code of Conduct: http://python.org/psf/codeofconduct/