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

Donald Stufft donald at stufft.io
Sat May 9 20:33:21 CEST 2015


> On May 9, 2015, at 2:30 PM, David Mertz <mertz at gnosis.cx> wrote:
> 
> On Sat, May 9, 2015 at 1:16 PM, Steven D'Aprano <steve at pearwood.info <mailto:steve at pearwood.info>> wrote:
> On Sat, May 09, 2015 at 11:38:38AM -0400, Ron Adam wrote:
> 
> > How about an operator for partial?
> >
> >           root @ mean @ map $ square(xs)
> 
> I have trouble seeing the advantage of a special function composition operator when it is easy to write a general 'compose()' function that can produce such things easily enough.
> 
> E.g. in a white paper I just did for O'Reilly on _Functional Programming in Python_ I propose this little example implementation:
> 
> def compose(*funcs):
>     "Return a new function s.t. compose(f,g,...)(x) == f(g(...(x)))"
>     def inner(data, funcs=funcs):
>         result = data
>         for f in reversed(funcs):
>             result = f(result)
>         return result
>     return inner
> 
> Which we might use as:
> 
>   RMS = compose(root, mean, square)
>   result = RMS(my_array)


Maybe functools.compose?

---
Donald Stufft
PGP: 7C6B 7C5D 5E2B 6356 A926 F04F 6E3C BCE9 3372 DCFA

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20150509/23b0dbd8/attachment.html>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 801 bytes
Desc: Message signed with OpenPGP using GPGMail
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20150509/23b0dbd8/attachment.sig>


More information about the Python-ideas mailing list