[Python-ideas] Function composition (was no subject)

Andrew Barnert abarnert at yahoo.com
Sat May 9 09:21:53 CEST 2015


On May 8, 2015, at 19:58, Stephen J. Turnbull <stephen at xemacs.org> wrote:
> 
> Koos Zevenhoven writes:
> 
>> As a random example, (root @ mean @ square)(x) would produce the right 
>> order for rms when using [2].
> 
> Hardly interesting. :-)  The result is an exception, as root and square
> are conceptually scalar-to-scalar, while mean is sequence-to-scalar.

Unless you're using an elementwise square and an array-to-scalar mean, like the ones in NumPy, in which case it works perfectly well...

> I suppose you could write (root @ mean @ (map square)) (xs),

Actually, you can't. You could write (root @ mean @ partial(map, square))(xs), but that's pretty clearly less readable than root(mean(map(square, xs))) or root(mean(x*x for x in xs). And that's been my main argument: Without a full suite of higher-level operators and related syntax, compose alone doesn't do you any good except for toy examples.

But Koos's example, even if it was possibly inadvertent, shows that I may be wrong about that. Maybe compose together with element-wise operators actually _is_ sufficient for something beyond toy examples.

Of course the fact that we have two groups of people each arguing that obviously the only possible reading of @ is compose/rcompose respectively points out a whole other problem with the idea. If people just were going to have to look up which way it went and learn it through experience, that would be one thing; if everyone already knows intuitively and half of them are wrong, that's a different story...

> which
> seems to support your argument.  But will all such issues and
> solutions give the same support?  This kind of thing is a conceptual
> problem that has to be discussed pretty thoroughly (presumably based
> on experience with implementations) before discussion of order can be
> conclusive.
> 
> _______________________________________________
> Python-ideas mailing list
> Python-ideas at python.org
> https://mail.python.org/mailman/listinfo/python-ideas
> Code of Conduct: http://python.org/psf/codeofconduct/


More information about the Python-ideas mailing list