[Python-ideas] Function composition (was no subject)
Koos Zevenhoven
koos.zevenhoven at aalto.fi
Fri May 8 09:03:28 CEST 2015
On 8.5.2015 8:19, Rustom Mody wrote:
> On Wed, May 6, 2015 at 6:45 PM, Ivan Levkivskyi <levkivskyi at gmail.com
> <mailto:levkivskyi at gmail.com>> wrote:
>
>
> def sunique(lst):
> return sorted(list(set(lst)))
>
> vs.
>
> sunique = sorted @ list @ set
>
>
> I would like to suggest that if composition is in fact added to python
> its order is 'corrected'
> ie in math there are two alternative definitions of composition
>
> [1] f o g = λ x • g(f(x))
> [2] f o g = λ x • f(g(x))
>
> [2] is more common but [1] is also used
>
> And IMHO [1] is much better for left-to-right reading so your example
> becomes
> sunique = set @ list @ sorted
> which reads as smoothly as a classic Unix pipeline:
>
> "Unnamed parameter input to set; output inputted to list; output
> inputted to sort"
>
>
While both versions make sense, [2] is the one that resembles the
chaining of linear operators or matrices, since column vectors are the
convention. For the left-to-right pipeline version, some other operator
might be more appropriate. Also, it would then be more clear to also
feed x into the pipeline from the left, instead of putting (x) on the
right like in a normal function call.
As a random example, (root @ mean @ square)(x) would produce the right
order for rms when using [2].
-- Koos
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20150508/8570f274/attachment-0001.html>
More information about the Python-ideas
mailing list